Remove extra padding in DataGridView column label?

2019-09-05 02:51发布

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?

1条回答
我想做一个坏孩纸
2楼-- · 2019-09-05 03:35

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
查看更多
登录 后发表回答