This is a migrated thread and some comments may be shown as answers.

RadGridView AutoSizeColumnMode & BestFitColumns

3 Answers 1693 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tyler
Top achievements
Rank 1
Tyler asked on 09 Sep 2016, 04:07 PM

Hello, 

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.ResetDataBinding();

...

...

grid.DataSource = newDataSource;

FormatGrid();

BestFitColumns(BestFitColumnsMode.AllCells);

 

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.

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Sep 2016, 12:00 PM
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.

Regards,
Dess
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.
0
SMc
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 01 Aug 2019, 11:15 PM
Where do I find the "BestFit" or "AutoSizeColumnMode" methods? I can't find them in the properties view or intellisense.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Aug 2019, 12:07 PM
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. 

Regards,
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.
Tags
GridView
Asked by
Tyler
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
SMc
Top achievements
Rank 2
Iron
Veteran
Iron
Share this question
or