3

I am working on a Windows Forms application and I have a DataGrid for which I am trying to increase the height of the column headers.

I know how its done for a datagridview but I am unsure about the DataGrid.

I have a column named Actual \n Qty, and this is being displayed as below:

enter image description here

May I know a way we could increase column header height?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
DoIt
  • 3,270
  • 9
  • 51
  • 103
  • It is CaptionFont.Size – Hans Passant Jul 07 '16 at 17:36
  • @HansPassant I think this just increases the size of text in the header but not the height of the header row? I have a column name in multiple lines but two lines doesn't fit in the current header row – DoIt Jul 07 '16 at 19:35
  • @Dev Is it really `.Net 1.1` or you just tagged with it because of `DataGrid`? If it's just because of `DataGrid`, you can change the tag to `.Net`. – Reza Aghaei Jul 08 '16 at 21:01

1 Answers1

3

Height of the column header in DataGrid is calculated based on HeaderFont property and is stored in a private filed headerFontHeight. You can get the field using reflection and change its value this way:

var p = typeof(DataGrid).GetField("headerFontHeight",
    System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
p.SetValue(dataGrid1, dataGrid1.HeaderFont.Height * 2);

var m = typeof(DataGrid).GetMethod("OnLayout",
    System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
m.Invoke(dataGrid1, new object[] { null });
dataGrid1.Invalidate();

enter image description here

You can assign the height which you think is enough or you can calculate the height of text of all columns and set the field to the maximum value.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • You must be the only one who know about this code! It is not intuitive at all! Congratulations on figuring it out. – NoChance Jan 16 '22 at 14:33
  • 1
    @NoChance I didn't find a better solution by searching in [source code](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/DataGrid.cs,5383) of [DataGrid](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagrid?view=netframework-4.8&WT.mc_id=DT-MVP-5003235). – Reza Aghaei Jan 16 '22 at 21:56
  • I thought I'd try "some\ntext" but for some reason it did not work in my case - Maybe your way is the only way! Stay safe. – NoChance Jan 16 '22 at 22:09