how to Best Fit(All Columns) in a GridControl

kashif picture kashif · Oct 5, 2012 · Viewed 18.6k times · Source

How to best fit all the columns in a grid control when form is loaded. I do have a button as shown in the following picture to do that when i right click the header of the grid control, but as opposed to this I want this event to be fired automatically when the form is loaded. I don't want to do this by right clicking the header of the grid control and than clicking the Best Fit(all columns) button to best fit all the columns. enter image description here

Answer

Stig picture Stig · Oct 5, 2012

This is how I do it.

if (view is GridView)
{
   // auto best fit...
   (view as GridView).BestFitMaxRowCount = 5000;   // just to avoid to many compares
   (view as GridView).BestFitColumns();
   foreach (GridColumn item in (view as GridView).Columns) // reduce the width of very wide columns
   {
      item.Width = (item.Width > 1000) ? 1000 : item.Width;
   }
}