views:

13

answers:

2

Using VB.Net I want to automatically size the height of the headers in a DataGridView so I am calling AutoResizeColumnHeadersHeight during the form load after the data has been populated.

The method doesn't appear to do anything, but does work if I do the following: - put a breakpoint on grid.AutoResizeColumnHeadersHeight() - open quickwatch on the grid - expand the child properties - close quickwatch and hit F5 to continue processing

The resizing then works!

I assume the reason is that evaluating one of the properties of the grid is causing the method to behave differently.

Does anybody know either what I should be doing differently, or a workaround - e.g. which property do I need to evaluate to make it work?

A: 

Found a workaround, the method works correctly if I add a line before like:

Dim workaround As System.IntPtr = grid.Handle
grid.AutoResizeColumnHeadersHeight()

I'd still be interested in a better answer though.

John Donoghue
+2  A: 

Are you 100% sure this code runs in the Load event? Using the Handle property ensures that the control window is created and that it will automatically scale to adjust to the machine's video adapter and system font settings. That should never be necessary if the code actually runs due to the Load event, all Handles of all controls will have been created by then.

Note that AutoResizeColumnHeadersHeight() is actually for manual resizing, it only works once. Although it automatically calculates the height. To get automatic resizing, set the ColumnHeadersHeightSizeMode property to AutoSize.

Hans Passant
Argh, you are correct, it's been moved to run in the InitializeComponent called from New.I do want manual resizing though, then let the user change things if they want to.
John Donoghue