|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DataGrid WidthHi 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 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 > > 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; } -- Show quoteTim Wilson ..Net Compact Framework MVP "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 > > 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 Huh. It all seemed to work fine for me.
-- Show quoteTim Wilson ..Net Compact Framework MVP "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. > > 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. -- Show quoteTim Wilson ..Net Compact Framework MVP "web1110" <we***@comcast.net> wrote in message news:UZqdnUz-atM229nfRVn-ow@comcast.com... > Although I do have readonly set. Not worth investigating. > >
Other interesting topics
|
|||||||||||||||||||||||