0
votes

I have a form that contains a datagridview. The cells in the datagridview expand or contract depending on text width.

How can I make the parent form in which the data grid is docked also contract or expand to the cell (total of all cells) width?

Hence I want the parent form to change in the row (width) size and not in the column (height) size?

Im using vb.net

1

1 Answers

1
votes

I would recommend just making it so that users can resize the program to the width that they want, and have the datagridview adjust it's width to fit the forms width. If you adjust it based on the columns, then it could cause issues where the form is bigger than the users screen. The easiest way to do this is by using the Dock and Anchor attributes.

If you really want to have the form adjust it's width to fit the columns, you would have to loop through the columns and add the width values together after it is databound. After you get the total width, you would have to set your forms width

The code would look something like this:

    Dim intWidth As Integer
    For Each dgvcc As DataGridViewColumn In DataGridView1.Columns
        intWidth += dgvcc.Width
    Next
    Me.Width = intWidth