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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #14317 |

    Hi I have an unbound master/detail grid (actually it nests deeper than that with some detail grids also acting as a master with there own detail grids).

    I can successfully set the Column Widths for the top level master using the GridControls Resize event but cannot seem to get it to affect the column widths on any DetailGrids.

    My end goal is to use GetFittedWidth() on all columns in each DetailGrid but the last column and size the last column to the left over space in that DetailGrid.

    What is the best approach for this scenario?

    Regards
    Geoff

    Imported from legacy forums. Posted by geoffhop (had 2929 views)

    User (Old forums)
    Member
    Post count: 23064

    One of the approaches I’ve used is to loop through each datarow. Each data row has a property detail grids. Select the detail grids for each data row and then resize that sub grid. A bit much but that’s the way I’ve done it. If you come up with a better way let me know.

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

    User (Old forums)
    Member
    Post count: 23064

    Hi uasking

    Thanks for the reply, it’s a technique similar to the one of the many I have tried. Did you place this code in a specific event? I’ve tried in both the master grids resize event and in the host forms resize event with no luck, it just seems to ignore any value I apply to the columns width property. The only time I seem to be able to get a value to stick is when I am adding the columns. It also seems that the Columns GetFittedWidth() method always returns zero when called against a column in a DetailGrid and if you use the cells parent columns GetFittedWidth it returns an index out of bounds exception!!

    Any other ideas greatly appreciated.

    Regards
    Geoff

    Imported from legacy forums. Posted by geoffhop (had 288 views)

    User (Old forums)
    Member
    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.
    if(c.Visible)
    {
    // 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;
    else
    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)
    innerSubGrid.ResizeCells();
    }
    }</PRE>

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.