Reputation: 6778
I have a column in a DataGridView
like so:
private System.Windows.Forms.DataGridViewTextBoxColumn TaskOrderAmountColumn;
this.TaskOrderAmountColumn.DataPropertyName = "Amount";
this.TaskOrderAmountColumn.HeaderText = "Task Order Amount";
this.TaskOrderAmountColumn.Name = "TaskOrderAmountColumn";
this.TaskOrderAmountColumn.ReadOnly = true;
this.TaskOrderAmountColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
The width of the column is too much. So I add this line:
this.TaskOrderAmountColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
This does decrease the width of the column, but it has the side effect of causing the column header to go to 2 rows:
How do I decrease the width of the column and keep the row header at 1 row?
The solution here has the same effect. This is not surprising since the root cause of the problem appears to be that the width is not correctly calculated from the text "Task Order Amount". However, by changing one line:
c.Width = w + 25;
i.e., adding just a little to the calculated with, it displays the header row as a single row.
Upvotes: 2
Views: 237
Reputation: 125312
To have single line column headers when setting AutoSizeMode
of the column to ColumnHeader
, set ColumnHeadersHeightSizeMode
property of DataGridView
to something different than AutoSize
, for example set it to DisableResizing
using designer or using code.
Also keep in mind that the ColumnHeadersHeight
property should not be taller than a single line of text (Which is not taller by default, unless you change the height or column header font).
Upvotes: 1