views:

2502

answers:

1

I have a Form and a DataGridView. I populate the DataGridView at runtime, so I want to know how do I resize the Form dynamically according to the size of the DataGridView? Is there any sort of property or method? Or do I have to determine the size myself and update accordingly?

+3  A: 

Normally controls adapt their sizes to the size of the containing form. To adjust the size of your form to the size of your DataGridView, you do have to determine the size yourself and then set the form's size to match, remembering to take into account the extra size required by the form's menu strip and/or toolbars, status bars or other controls.

In your case, it would probably be best not to resize the form to match the grid view control. Most likely, you will have many more rows in your grid view than could fit on your Windows screen, and you don't want to have a form that extends below the viewable desktop area. Generally speaking, this type of situation is exactly why you want to have a scrollable grid view - for viewing more data than can fit on the screen at one time.

MusiGenesis
"To adjust the size of your form to the size of your DataGridView, you do have to determine the size yourself"Is there a way to do that please? My DataGridView only has a few cells and it read-only. Resizing the form to fit is no problem at all, except I don't know how many pixels the DGV needs.Many thanks.
billpg