[Expand]General Information
[Collapse]WinForms Controls
  .NET Core Support
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications, and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Collapse]Data Grid
   [Collapse]Get Started With Data Grid and Views
     [Expand]Appearance and Conditional Formatting
     [Expand]Data Editing
     [Expand]Hit Information
     [Expand]Data Binding and Working with Columns
     [Collapse]Grid View - Columns, Rows and Cells
       Tutorial: Column Resizing
       Tutorial: Best Fit Option
       Tutorial: Auto Width Mode
       Tutorial: Fixed Columns
       Tutorial: Column Reordering
       Tutorial: Column Visibility
       Tutorial: Column Chooser API
       Tutorial: Auto Row Height
       Tutorial: Cell Merging
       Tutorial: Identifying Rows
       Tutorial: Row Height and Layout Basics
     [Expand]Banded Views
     [Expand]WinExplorer View
     [Expand]Tile View
     [Expand]Filter and Search
     [Expand]Split Presentation
     [Expand]Row Preview Sections
   [Expand]Data Binding
    Unbound Columns
   [Expand]Data Editing and Validation
   [Expand]Filter and Search
   [Expand]Focus and Selection Handling
    Format Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
    Split Presentation
    Row Preview Sections
   [Expand]Batch Modifications
    Hit Information
    Popup Menus
   [Expand]Save and Restore Layout
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Property Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Gantt Control
  [Expand]Chart Control
  [Expand]Map Control
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Sunburst Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Tutorial: Best Fit Option

This walkthrough is a transcript of the Best Fit Option video available on the DevExpress YouTube Channel.

Expanded Default Behavior

The grid View provides built-in column header menus where you can select the Best Fit option. This adjusts a column's width to completely display its content.

The same can be achieved by double-clicking a column's right edge. To adjust all columns at once, use the Best Fit (all columns) option from the column header menu.

Expanded Applying the Best Fit Option at Design Time

The same menus are available at design time, but the mentioned menu items or a header edge double-click will have no effect, since data isn't loaded. To work around this, you will need to open the grid's Designer dialog and switch to the Layout Page. This is where you can pre-load data and then apply the best fit width to columns.

Expanded Limiting the Number of Records to Apply Best Fit

By default, when you apply the best fit width to a column, the View checks values in all rows. This might have an unnecessary impact on performance with larger data sources. To limit the number of records the grid View scans, use the GridOptionsView.BestFitMaxRowCount property under GridView.OptionsView. In this example, set it to 3 and apply the best fit width again to see that the result differs.

Expanded Disabling the Best Fit Option

If you disable column resizing, the Best Fit option gets disabled as well – both via the context menu or by double-clicking a column header's right edge.

Expanded Forcing a Column to Have a Fixed Width

Restore default values for all changed options and set a column's OptionsColumn.FixedWidth option. This means that the corresponding column will keep its width when you apply the Best Fit feature to the view.

Expanded Applying Best Fit in Code

Add three buttons to the UI to see how you can use this feature in code. One button will call the Grid View's GridView.BestFitColumns method that affects all columns. The second uses a column's GridColumn.BestFit method to apply the feature to an individual column. The third method gives you the most flexibility, since you can calculate the width using the GridColumn.GetBestWidth method and then adjust the value before actually applying the change to a column.

You can now run the application and see how all three methods work at runtime.

Expanded See Also

Is this topic helpful?​​​​​​​