Home Forums WinForms controls Xceed Grid for WinForms Autofit Columns

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

    Hi,

    I have searched this forum and the help files extensively but cannot find a definitive answer so I hope someone can help me…

    What is the best way to autofit columns in a databaound grid?

     I use this…

     Foreach (Column column in this.xdgData.Columns)

    {

    if (column.GetFittedWidth() != -1)

    { column.Width = column.GetFittedWidth(); }

    }

    but I am not sure where the best place or event to call this from. Obvioulsy I want it to autofit when data is first loaded and then again if a user changes a field. I have tried datatcontext changed event but this is called before the grid is actually drawn.

    Thanks in advance.

      

     

    Imported from legacy forums. Posted by Phil (had 3285 views)

    User (Old forums)
    Member
    Post count: 23064

    Most likely at Form_Load, after the data has loaded in the grid, and in the Cell.ValueChanged event, that will be triggered after the user has edited a cell.  In this event handler, you would only need to do this on the ParentColumn of the cell, not on all columns.

    Imported from legacy forums. Posted by André (had 1916 views)

    User (Old forums)
    Member
    Post count: 23064

    I found the best way to do this was to run the following against the column after the DataSource has been assigned (“control” is and Xceed.Grid.GridControl)

    control.Columns[“ColumnName”].Width = control.DataRows.Cast<Xceed.Grid.DataRow>().Max(r => r.Cells[“ColumnName”].GetFittedWidth());

    Note: you need to be using Visual Studio 2008 or above and use the LINQ assemblies….

    Craig

    Imported from legacy forums. Posted by Craig (had 587 views)

    User (Old forums)
    Member
    Post count: 23064

    See: http://www3.xceed.com/CS/forums/permalink/25546/25546/ShowThread.aspx#25546

    This might help.

    Imported from legacy forums. Posted by Neo (had 1350 views)

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