KDecker
KDecker

Reputation: 7148

Remove extra padding in DataGridView column label?

I have a DataGridView that I am trying to reduce the width of so it displays how I wish in my form. I am having issues "removing" the extra padding in the column names when reducing the size of the DataGridView.

enter image description here

On the left is the table as I originally had it, I realized that I would like it smaller so I decreased its width. The center image represents the smallest the columns will go before the text wraps to the next line. The right image is the size I would like the table to be, I have shown that the text "(px)" will fit in the space.

I have gone through all of the settings I can find in the designer and have not found anything to help. Does anyone know how to fix this?

Upvotes: 1

Views: 1682

Answers (1)

jhmt
jhmt

Reputation: 1421

Here is my example. Although it might not be the best solution but worked.

I measured the exact width of HeaderCell's content ("Height (px)") and set the column's width with some additional padding (For example, 9).

dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;
Size textSize = TextRenderer.MeasureText(dataGridView1.Columns[0].HeaderText, dataGridView1.Font);
dataGridView1.Columns[0].Width = textSize.Width + 9; // Adding some padding

Upvotes: 2

Related Questions