DataGridView AutoFit and Fill

I have 3 columns in my DataGridView . What I'm trying to do is make the first 2 columns automatically match the width of the content and the third column fill the remaining space.

Can this be done in WinForms? I am loading my data from an EF DataContext, if used. I included an image of what it currently looks like.

enter image description here

+75
c # winforms datagridview
Sep 06 '13 at 21:04 on
source share
8 answers

You need to use the DataGridViewColumn.AutoSizeMode property.

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

AllCells: The column width is adjusted to match the contents of all cells in the column, including the header cell.
AllCellsExceptHeader: The column width is adjusted to match the contents of all cells in the column, excluding the header cell.
DisplayedCells: The column width is adjusted to match the contents of all the cells in the column that are currently in the rows displayed on the screen, including the header cell.
DisplayedCellsExceptHeader: The column width is adjusted to match the contents of all the cells in the column that are currently in the rows displayed on the screen, excluding the header cell.

Then you use the Fill value for column 2

The column width is adjusted so that the width 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 other users note, the default value can be set at the datagridview level using the DataGridView.AutoSizeColumnsMode property.

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

may be:

 this.DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; 



Important Note:

If your grid is bound to a data source and the columns are automatically generated ( AutoGenerateColumns property set to True), you need to use the DataBindingComplete event to apply AFTER style columns.




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

+148
Sep 06 '13 at 21:27
source share

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; } }; 
+20
Jul 28 '15 at 19:54
source share

Just change the property from the control property: AutoSizeColumnsMode:Fill

OR by code

dataGridView1.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.Fill;

+8
Jun 25 '15 at 11:59
source share

Not verified, but you can try. Tested and working. I hope you can play with AutoSizeMode DataGridViewColum to achieve what you need.

Try to install

 dataGridView1.DataSource = yourdatasource;<--set datasource before you set AutoSizeMode //Set the following properties after setting datasource dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; 

This should work

+4
Sep 06 '13 at 21:15
source share

Try to do

  AutoSizeColumnMode = Fill; 
0
Dec 31 '15 at 6:10
source share
 public static void Fill(DataGridView dgv2) { try { dgv = dgv2; foreach (DataGridViewColumn GridCol in dgv.Columns) { for (int j = 0; j < GridCol.DataGridView.ColumnCount; j++) { GridCol.DataGridView.Columns[j].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; GridCol.DataGridView.Columns[j].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; GridCol.DataGridView.Columns[j].FillWeight = 1; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } 
0
Feb 17 '15 at 1:48
source share
 public void setHeight(DataGridView src) { src.Height= src.ColumnHeadersVisible ? src.ColumnHeadersHeight : 0 + src.Rows.OfType<DataGridViewRow>().Where(row => row.Visible).Sum(row => row.Height); } 
-one
Dec 03 '13 at 13:18
source share

Try the following:

  DGV.AutoResizeColumns(); DGV.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.AllCells; 
-2
Jan 19 '17 at 14:32
source share



All Articles