RadGridView AutoSizeColumnMode & BestFitColumns

2 posts, 0 answers
  1. Tyler
    Tyler avatar
    17 posts
    Member since:
    Jun 2016

    Posted 09 Sep 2016 Link to this post


    I have an application that is using the RadGridView.

    The initialization of the Grid sets "AutoColumnSizeMode = GridViewAutoColumnSizeMode.Fill".

    The very first time the grid is populated, formatted, and then BestFitColumns(BestFitColumnsMode.AllCells) is called, and everything looks good.


    The issue arises after this "first" grid fill.

    When we refresh the grid with a new databsource and reformat the grid, again the BetstFitColumns(BestFitColumnsMode.AllCells) is called.

    Process is as follows:

    grid.DataSource = null;




    grid.DataSource = newDataSource;




    We are using a data-source which initially includes a lot of columns, but upon 'formatting' the grid we are also limiting the visible columns by hiding them all, then showing the select few that we need.


    The attachment shows what the result is after the first grid fill...  looks awful.


    To further my confusion, I have added a checkbox turn on / off BestFit (BestFitColumns(BestFitColumnsMode.AllCells / None), and after 3-4 times of hitting this, it looks good again.  Refresh datasources again and everything is back to messed up.


    How in the world do i get this to be consistent the first time and every time after that??

    I hope I have explained the issue with enough detail.  Let me know if you have question.

  2. Dess
    Dess avatar
    2103 posts

    Posted 13 Sep 2016 Link to this post

    Hello Tyler,

    Thank you for writing.  

    RadGridView supports two ways to auto-size the columns:

    - AutoSizeColumnsMode
    : columns can automatically fill the entire width of the grid. Just set the AutoSizeColumnsMode property of the desired template to GridViewAutoSizeColumnsMode.Fill.

    - Best fit: the column widths can be set to fit its content by using the GridViewTemplate.BestFitColumns or GridViewDataColumn.BestFit methods. This mode distributes algorithm that attempts to fit in the header text and column data for all visible rows.

    Mixing the two ways is not possible. However, you can best fit the columns and manually adjust the last column's width according to the remaining space. You can subscribe to the RadGridView.SizeChanged event and recalculate the last column's width.

    I hope this information helps. Should you have further questions I would be glad to help.

    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top