RadGridView AutoSizeColumnMode & BestFitColumns

4 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 | Tech Support Engineer, Sr.
    Dess | Tech Support Engineer, Sr.  avatar
    3568 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.
  3. SMc
    SMc avatar
    1 posts
    Member since:
    Oct 2018

    Posted 01 Aug 2019 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Where do I find the "BestFit" or "AutoSizeColumnMode" methods? I can't find them in the properties view or intellisense.
  4. Dess | Tech Support Engineer, Sr.
    Dess | Tech Support Engineer, Sr.  avatar
    3568 posts

    Posted 06 Aug 2019 Link to this post

    Hello, Sean, 

    The AutoSizeColumnsMode property is relevant for RadGridView. RadGridView also offers the BestFitColumns method.

    Each column in RadGridView can be best-fit separately by calling the GridViewColumn.BestFit method. Additional information about resizing columns is available in the following help article: https://docs.telerik.com/devtools/winforms/controls/gridview/columns/resizing-columns-programatically

    I hope this information helps. 

    Dess | Tech Support Engineer, Sr.
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top