Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView AutoFit and Fill

I have 3 columns in my DataGridView. What I am trying to do is have the first 2 columns auto fit to the width of the content, and have the 3rd column fill the remaining space.

Is it possible to do in WinForms? I am loading my data from an EF DataContext if that's any use. I have included an image of how it currently looks.

enter image description here

like image 788
James Jeffery Avatar asked Sep 06 '13 21:09

James Jeffery


People also ask

How to fit columns in DataGridView in c#?

You need to use the DataGridViewColumn. AutoSizeMode property. You can use one of these values for column 0 and 1: AllCells: The column width adjusts to fit the contents of all cells in the column, including the header cell.

How to adjust DataGridView column width in c#?

To adjust column widths programmatically, use the AutoResizeColumn or AutoResizeColumns methods or set the column Width property. For more information about content-based automatic sizing, see Sizing Options in the Windows Forms DataGridView Control.


2 Answers

You need to use the DataGridViewColumn.AutoSizeMode property.

You can use one of these values for column 0 and 1:

AllCells: The column width adjusts to fit the contents of all cells in the column, including the header cell.
AllCellsExceptHeader: The column width adjusts to fit the contents of all cells in the column, excluding the header cell.
DisplayedCells: The column width adjusts to fit the contents of all cells in the column that are in rows currently displayed onscreen, including the header cell.
DisplayedCellsExceptHeader: The column width adjusts to fit the contents of all cells in the column that are in rows currently displayed onscreen, excluding the header cell.

Then you use the Fill value for column 2

The column width adjusts so that the widths of all columns exactly fills the display area of the control...

this.DataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; this.DataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; this.DataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; 

As pointed out by other users, the default value can be set at datagridview level with DataGridView.AutoSizeColumnsMode property.

this.DataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; this.DataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; 

could be:

this.DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; 

Important note:

If your grid is bound to a datasource and columns are auto-generated (AutoGenerateColumns property set to True), you need to use the DataBindingComplete event to apply style AFTER columns have been created.


In some scenarios (change cells value by code for example), I had to call DataGridView1.AutoResizeColumns(); to refresh the grid.

like image 145
Chris Avatar answered Sep 24 '22 18:09

Chris


This is my favorite approach...

_dataGrid.DataBindingComplete += (o, _) =>     {         var dataGridView = o as DataGridView;         if (dataGridView != null)         {            dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;            dataGridView.Columns[dataGridView.ColumnCount-1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;         }     }; 
like image 26
AlfredBr Avatar answered Sep 24 '22 18:09

AlfredBr