tags:

views:

2086

answers:

2

In C# how do you make the columns in a DataGrid AutoFit Column Width like you can in Excel? Currently my five columns are fixed width but the column headers can change so I would like the columns to autofit to the width of the column.

Thanks

+4  A: 

There's a property on the DataGridView called AutoSizeColumnsMode which is an enum. The available values are:

AllCells

AllCellsExceptHeader

ColumnHeader

DisplayedCells

DisplayedCellsExceptHeader

Fill

None

BFree
+1  A: 

To take BFree's answer a step further:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.autoresizecolumns.aspx

Jeremy Bade