My question is very simple – after data binding I want to set all columns width so that it will not be horizontal scrolling. Something like this:
columnWidth = grid.width/grid.Columns.Count
But this expression does not consider the "left part of gridView" with which I can select row, which displays current row. How can I calculate its width? [(grid.width – X)/grid.Columns.Count]
Very easy to do this. Just set the DataGridView.AutoSizeColumnMode property to
Fill.And you may need to explicitly ask the grid to resize column spacing. I can’t remember for sure. But that is simple to do:
According to the documentation,
Fillwill do the following:So basically, as long as you don’t have any columns with a
MinimumWidththat will prevent it, this property does exactly what you are looking for.EDIT: Addressing the comment of the OP:
You can always create a simple property to report that information to you. Something like…
You could also use this to inherit from a
DataGridViewand add the property yourself to the control. Note that there is probably some other dead space from the borders of theDataGridViewthat you need to account for.