views:

470

answers:

1

in one of my forms a datagridview displays data from a database (of course the number of data (so number of rows) can change). The database connection is in the form load event. I just cant figure out how the height of the whole datagridview is autosized, depending on the number of rows it displays.

A: 

If you set DataGridView.AutoSize == true then as you add more rows it gets longer. Otherwise you get scrollbars. Unless you've set ScrollBars == Null || Horizontal in which case the rows just disappear of of the end.

Although DataGridView.AutoSize can only be set programmatically, for some reason. And there's some odd things that happen when you put the grid inside an autosizable control. It doesn't seem to respond to the size of the grid.

I ended up calulating the expected size of the grid from the column, row, header, margin, padding and border sizes, and programmatically the controls containing the grid and anchoring the grid on four sides. Felt really clunky but it's all I could find. If you're still around, comment and I'll see if I can find the code, I don't have it on hand.

Spike