resize a form when datagrid.height increases

JohnDW

Well-known member
Joined
Jun 13, 2012
Messages
60
Location
Antwerp, Belgium
Programming Experience
1-3
Hello,

I'm looking for some help.
In a form that I want to
print,
I have a datagridview (gets filled with a loop)
that increases (growing in height) if new rows are added.
VB.NET:
            Do While dr.Read = True
                dgw.Rows.Add(dr("Aantal"), dr("Productomschrijving"), dr("CategorieId"), dr("Maatid"), dr("Verkoopprijs"), dr("TotalPrice"))
               
                dgw.Height += 19 'this resizes the datagridview in the form : this works!
                x += 19  'should resize the form height (but doesn't)
                ActiveForm.Height += 19  'try to resize the form height (but doesn't work also)
 
                lblLine.Height += 19 '(those labels should move according to the growth of the datagridview
                lblOk.Height += 19  'doesn't work also
                lblThank.Height += 19  'doesn't work also
               
            Loop

The problem I have is: the form doesn't increase, the datagridview does
VB.NET:
 dgw.Height += 19
That part of code resizes the datagridview in the form : this works!

. I'll tried to do resize the form with
VB.NET:
ActiveForm.Height += 19
.
BUt this doesn't work.

The question is how do I make the form height grow within this loop,
also some label on the bottom of the form must be resized according the
grow of the datagridview.
(lblLine.Height , lblOk.Height , lblThank.Height ).

I would be grateful.

Txs.
 
You really are making life hard for yourself on a number of levels.

Firstly, if you want to print that data then you should not simply take a screen shot of the grid, as I imagine you are doing. You should print the data, not the form. If you use a PrintDocument then you can print whatever you want, however you want. Now, that actually doesn't make your life easier because you would have to write all the printing code BUT it just so happens that I know someone who has created a class that will use a PrintDocument to print the entire contents of a DataGridView. You can find that class here. If you use that then the size of the grid and the form are irrelevant because what's shown on the screen and what's printed are not related.

Now, if you don't want to use that but prefer to stick with the way you're already printing, you should still change some things. Firstly, get rid of that loop. You should create a DataTable, Load the data reader into it and then bind that table to the grid. All the data gets loaded in one line of code that assigns the DataTable to the DataSource of the DataGridView. You don't have to increase the Height of the grid incrementally because you can simply get the number of rows and then get the total increase in height by a single multiplication.

Even then though, you should NOT change the Height of the grid directly. What you should do is set the Anchor property of the grid to anchor it to the Top, Left and Bottom of its parent and, if you might want to change the Width too, anchor it to the Right as well. You then make one change to the Height of the form and the grid will resize automatically to ensure that the distance of its edges from the edges of the form that it's anchored to remain constant. If you want to see an example of that, check out this thread of mine.
 
Back
Top