473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

DataGrid Width

Hi y'all,

I have resized the columns in a DataGrid and I want to set the width of the
DataGrid to fit the columns. Just summing the column widths is too short
due to the grid and gray row selection column on the left.

I have the widths of the columns. What other values do I need to include in
the DataGrid width?

Thanx,
Bill
Nov 17 '05 #1
9 3919
JV
Are you talking about an ASP.NET datagrid or a Windows Datagrid?

"web1110" <we***@comcast.net> wrote in message
news:N_********************@comcast.com...
Hi y'all,

I have resized the columns in a DataGrid and I want to set the width of
the
DataGrid to fit the columns. Just summing the column widths is too short
due to the grid and gray row selection column on the left.

I have the widths of the columns. What other values do I need to include
in
the DataGrid width?

Thanx,
Bill

Nov 17 '05 #2
A Windows DataGrid.
Nov 17 '05 #3
Since the DataGrid draws its own borders and the vertical scroll bar is a
child control that sits on top of the client area, you'll need to consider
the BorderStyle, RowHeaderWidth, column widths, and vertical scroll bar
width (if present). To retrieve the column widths, the code below assumes
that the DataGrid is using a custom DataGridTableStyle. If this is not the
case then you may need to make some tweaks to the source.

using System.Reflection;

private void AutoSizeDataGridWidth(DataGrid grid)
{
int width = 0;

// Calculate occupied border space.
switch (grid.BorderStyle)
{
case BorderStyle.None:
{
// Do nothing.
break;
}
case BorderStyle.FixedSingle:
{
width += 4;
break;
}
case BorderStyle.Fixed3D:
{
width += (SystemInformation.Border3DSize.Width * 2);
break;
}
}

// Calculate occupied row header space.
if (grid.TableStyles.Count > 0)
{
if (grid.TableStyles[0].RowHeadersVisible)
{
width += grid.RowHeaderWidth;
}
}
else if (grid.RowHeadersVisible)
{
width += grid.RowHeaderWidth;
}

// Calculate occupied column space.
if (grid.TableStyles.Count > 0)
{
foreach (DataGridColumnStyle column in
grid.TableStyles[0].GridColumnStyles)
{
width += column.Width;
}
}

// Calculate occupied vertical scrollbar space.
PropertyInfo propInfo = grid.GetType().GetProperty("VertScrollBar",
BindingFlags.Instance | BindingFlags.NonPublic);
if (propInfo != null)
{
ScrollBar propValue = propInfo.GetValue(grid, null) as ScrollBar;
if (propValue != null)
{
if (propValue.Visible)
{
width += propValue.Width;
}
}
}

grid.Width = width;
}

--
Tim Wilson
..Net Compact Framework MVP

"web1110" <we***@comcast.net> wrote in message
news:N_********************@comcast.com...
Hi y'all,

I have resized the columns in a DataGrid and I want to set the width of the DataGrid to fit the columns. Just summing the column widths is too short
due to the grid and gray row selection column on the left.

I have the widths of the columns. What other values do I need to include in the DataGrid width?

Thanx,
Bill

Nov 17 '05 #4
Thank you much.

Without a horizontal scroll bar present, I still had to add 3 to the width
to zap te horizonatl scroll bar, but all in all, it's pretty close.

Thanx,
Bill
Nov 17 '05 #5
er, I should add:

I had to add 3 to the width per column.
Nov 17 '05 #6
Huh. It all seemed to work fine for me.

--
Tim Wilson
..Net Compact Framework MVP

"web1110" <we***@comcast.net> wrote in message
news:u5********************@comcast.com...
er, I should add:

I had to add 3 to the width per column.

Nov 17 '05 #7
Some mysteries will never be solved. Thanks.
Nov 17 '05 #8
Although I do have readonly set. Not worth investigating.
Nov 17 '05 #9
Even with ReadOnly set to true it still works as expected at my end. Odd
problem. Must be something set slightly different with the DataGrid,
DataGridTableStyle, DataGridColumnStyle's, or the calculation of the column
widths.

--
Tim Wilson
..Net Compact Framework MVP

"web1110" <we***@comcast.net> wrote in message
news:UZ********************@comcast.com...
Although I do have readonly set. Not worth investigating.

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Billy Jacobs | last post by:
I am using a datagrid to display some data. I need to create 2 header rows for this grid with columns of varying spans. In html it would be the following. <Table> <tr> <td colspan=8>Official...
5
by: Prateek | last post by:
Hi, How can I change the width of a column in datagrid control? TIA Prateek
2
by: Josef Meile | last post by:
Hi, I'm using a ComboBox, some Textboxes, and a DataGrid to represent a many-to-many relationship between Person and Course. Each time that I change the value in the ComboBox (which for now is...
5
by: John M | last post by:
Hello, In Visual Studio .NET 2003, How can I change the columns' width (at design or runtime) of datagrid ? Thanks :)
2
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
4
by: Steve | last post by:
I am fairly new to VB.NET, and I am rewriting an application I wrote a while back, also in VB.NET. I aplied some new things I learned. Anyway, here is my problem....... I have a custom DataGrid...
6
by: Agnes | last post by:
I understand it is impossible, but still curious to know "Can I freeze several column in the datagrid, the user can only scroll the first 3 columns (not verical), for the rest of the coulumn, it is...
10
by: amiga500 | last post by:
Hello, I have one basic simple question. When I have multiple records in the datagrid as follows: Code Product 1 Product 2 Product 3 11111 A B C...
2
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.