lundi 20 avril 2015

DataGridView column widths return 100 before DataGridView is visible

I'm adding a DataGridView to a non-selected tab of a TabControl and I am manually sizing the last n columns to fill the remaining space. The first three columns which are static, use AutoSizeMode AllCells.

What I'm trying to do is, get the DataGridView width and subtract 19 for the vertical scrollbar, and then subtract the widths of the first three columns before dividing the remaining width up by n and assigning each of those columns that remaining width. When I add the DataGridView, I have some output which shows (the DataGridView is not visible at this time):

Initial Available Width: 866
Updated Available Width: 766
Updated Available Width: 666
Updated Available Width: 566
Remaing Columns Width: 94

The initial three columns each report a width of 100 which is incorrect. When I go to the tab the DataGridView is on and the DataGridView Resize event fires I get:

Initial Available Width: 1074
Updated Available Width: 974
Updated Available Width: 874
Updated Available Width: 774
Remaing Columns Width: 129

Still showing that 100 width for the first three columns and as a result, the horizontal scrollbar is visible due to the width of the last n columns exceeding the actual available space. When on the tab, I have some test code to get the current widths:

DGV Width  : 1093
Col 1 Width: 411
Col 2 Width: 40
Col 3 Width: 36
Col 4 Width: 129
Col 5 Width: 129
Col 6 Width: 129
Col 7 Width: 129
Col 8 Width: 129
Col 9 Width: 129

So everything is working as expected, except for the first three columns not reporting their true width. However, when I do a slight manual resize on the form which fires the DataGridView Resize event, I get the following:

Initial Available Width: 1074
Updated Available Width: 663
Updated Available Width: 623
Updated Available Width: 587
Remaing Columns Width: 97

So it's like when the DataGridView isn't actually visible, it can't calculate the widths of AutoSizeMode AllCells?

I've tried calling AutoResizeColumns() but it doesn't change anything, and AutoSizeMode Fill doesn't meet my requirements for the last n columns.

Aucun commentaire:

Enregistrer un commentaire