Nuno
Nuno

Reputation: 81

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.

Upvotes: 8

Views: 10591

Answers (4)

Michele Vazquez
Michele Vazquez

Reputation: 1

Try this:

this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

you can change it in the Designer of your Form to see the changes during the building process of the form.

This works well for me.

Upvotes: 0

Abdul Latheef
Abdul Latheef

Reputation: 1

this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
this.dataGridView1.ColumnHeadersHeight = 40;
this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

Upvotes: -1

JBA
JBA

Reputation: 35

Try it :)

this.dataGridViewMain.EnableHeadersVisualStyles = false;
this.dataGridViewMain.ColumnHeadersHeight = 60;

Upvotes: 0

Irshad
Irshad

Reputation: 3131

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

Upvotes: 10

Related Questions