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.
Best Answer
You need to use the
DataGridViewColumn.AutoSizeMode
property.You can use one of these values for column 0 and 1:
Then you use the Fill value for column 2
As pointed out by other users, the default value can be set at
datagridview
level withDataGridView.AutoSizeColumnsMode
property.could be:
Important note:
If your grid is bound to a datasource and columns are auto-generated (
AutoGenerateColumns
property set to True), you need to use theDataBindingComplete
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.