Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView.AutoResizeColumns is ignoring column header height

I have a DataGridView which columns I want resized to the biggest cell from all that column's items, including the header. I have this:

dataGridViewMain.ColumnHeadersHeight = 60;
dataGridViewMain.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

Why does that code result in a few columns looking like this:

There is clearly more space reserved than necessary and I checked all the rows. None contain more than 3 characters. This behavior also happens on various columns. It looks like AutoResizeColumns calculates the space required without taking into account that the column header has a size of 60 and can accommodate various rows of text.

like image 548
Nuno Avatar asked Dec 15 '15 03:12

Nuno


1 Answers

Your grid header is re-sizing to the header text. There is a property in dataGridViewMain you need to adjust.

Set as follows;

dataGridViewMain.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

Also this can be set via Property Window;

Resize

like image 127
Irshad Avatar answered Nov 10 '22 02:11

Irshad