Home Forums WinForms controls Xceed Grid for WinForms How to set Column Widths on DetailGrids? Reply To: How to set Column Widths on DetailGrids?

User (Old forums)
Post count: 23064

I know. What I’ve ended up doing is adding a button which allows the user to resize all the columns. The reason for this is that if a user resizes a column smaller, they might not want it to expand without knowing when they resize the form or move the scroll bars etc. Then when they close the grid, all the column widths are saved to their preferences in a database so that on loading the form, the columns are resized back to what they set them to. My resize code is just a little helper to get their columns resized.

It all depends I guess on how you’re adding your data and why you want them to resize automatically. As for the exception I have no idea.

The way I did it was to inherit from both the grid control and detail grids and add the follwing code:

To the grid control and detail grid:

<PRE>/// <summary>
/// Resize all the columns in this grid control to their maximum widths
/// according to their content.
/// </summary>
public void ResizeCells()
// Loop through each column and resize
foreach(Xceed.Grid.Column c in Columns)
// The gird doesn’t get the correct size of an item if it’s not
// visible. I don’t know why.
// Get the fitted width
int fittedWidth = c.GetFittedWidth();
int minWidth = System.Math.Max(c.MinWidth, 3);
int maxWidth = c.MaxWidth;

if(fittedWidth < minWidth)
c.Width = minWidth;
else if((-1 != maxWidth) && (fittedWidth > maxWidth))
c.Width = maxWidth;
c.Width = fittedWidth;

// Resize all the columns in any detail grids
foreach(Xceed.Grid.DataRow subRow in DataRows)
if(0 != subRow.DetailGrids.Count)
foreach(MyDetailGrid innerSubGrid in subRow.DetailGrids)

Imported from legacy forums. Posted by Chris (had 4153 views)