By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,125 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,125 IT Pros & Developers. It's quick & easy.

DataGrid Width

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
A Windows DataGrid.
Nov 17 '05 #3

P: n/a
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

P: n/a
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

P: n/a
er, I should add:

I had to add 3 to the width per column.
Nov 17 '05 #6

P: n/a
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

P: n/a
Some mysteries will never be solved. Thanks.
Nov 17 '05 #8

P: n/a
Although I do have readonly set. Not worth investigating.
Nov 17 '05 #9

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.