Home All Groups Group Topic Archive Search About
Author
25 Mar 2005 4:02 AM
web1110
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

Author
25 Mar 2005 3:25 PM
JV
Are you talking about an ASP.NET datagrid or a Windows Datagrid?

Show quote
"web1110" <we***@comcast.net> wrote in message
news:N_mdnV3z97nIEN7fRVn-qg@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
>
>
Author
25 Mar 2005 3:38 PM
web1110
A Windows DataGrid.
Author
25 Mar 2005 3:48 PM
Tim Wilson
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

Show quote
"web1110" <we***@comcast.net> wrote in message
news:N_mdnV3z97nIEN7fRVn-qg@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
>
>
Author
25 Mar 2005 4:38 PM
web1110
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
Author
25 Mar 2005 4:40 PM
web1110
er, I should add:

I had to add 3 to the width per column.
Author
25 Mar 2005 4:52 PM
Tim Wilson
Huh. It all seemed to work fine for me.

--
Tim Wilson
..Net Compact Framework MVP

Show quote
"web1110" <we***@comcast.net> wrote in message
news:u5mdnQgeINmSotnfRVn-jA@comcast.com...
> er, I should add:
>
> I had to add 3 to the width per column.
>
>
Author
25 Mar 2005 5:03 PM
web1110
Some mysteries will never be solved.  Thanks.
Author
25 Mar 2005 5:13 PM
web1110
Although I do have readonly set.  Not worth investigating.
Author
25 Mar 2005 5:23 PM
Tim Wilson
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

Show quote
"web1110" <we***@comcast.net> wrote in message
news:UZqdnUz-atM229nfRVn-ow@comcast.com...
> Although I do have readonly set.  Not worth investigating.
>
>

AddThis Social Bookmark Button