Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Resize Datagridview columns on form resize
 

Resize Datagridview columns on form resize

Hello,

I have four columns in a datagridview and at run-time, I want to resize the form in order to display all data for each of the columns. ie, by setting the DataGridViewAutoSizeColumnMode.AllCells property. I know I can use .Fill but if the columns contain data too wide for the form size, how can I set the minimum form size to the width of all four formatted columns in order to prevent the user from having to resize the form on load?

Thanks,
James
James4291  Tuesday, August 18, 2009 3:59 PM
Hello,

You will have to set the width of form to the width of datagridview (All columns resides within it).

If you want to prevent the user from resizing the form you can set it to fixed window style.


Note: - Since you are applying theDataGridViewAutoSizeColumnMode to AllCells, the whole data within the cell will be displayed...

If it will exceed you will get the scrollbar Bydefault... So don't worry..


Regsrds,,,,


Nothing is Impossible.....
R.KUMAR  Thursday, August 20, 2009 1:32 PM
Hello,

Thanks for your reply but I would like to allow the user to resize the form and with it, increase the width of one or all of the datagridview columns.

Thanks,
James
James4291  Thursday, August 20, 2009 1:52 PM
Hi!!!,

When we take a simple Windows form a user can resize it.

What you need to do is to resize your Datagridview.

If you have only the datagridview on a form you can set its dock property to fill and it will be adjusted automatically in whole form.

If you have more than one control on the form you can use the TableLayoutContainer. First Dock(Fill) it to Form and then Put your controls within it and dock then within the Cells.

There is nothing to do more.

==========
Kumar

Nothing is Impossible.....
R.KUMAR  Friday, August 21, 2009 7:03 AM
Thanks Kumar, but this still doesn't resize the form if the total width of all columns is greater than the form size at form_load.
James4291  Friday, August 21, 2009 3:57 PM
Hey James,

The width of your columns is based on the content [ Since theDataGridViewAutoSizeColumnMode is set to AllCells ] and it could be of any size. But your screen has a fixed width as well as every window has a width limit of 1036.

Further You can try this....
1) Place your Datagridview at the top-Left Corner i.e. at {0,0} position
2) Fill your Datagridview
3) Write the following code to resize the DatagridView---
'======================================================
Me.DataGridView1.Width = 0
Me.DataGridView1.Height = 0
For Each column As DataGridViewColumn In Me.DataGridView1.Columns
Me.DataGridView1.Width += column.Width
Next
Me.DataGridView1.Width += Me.DataGridView1.RowHeadersWidth
For Each Rows As DataGridViewRow In Me.DataGridView1.Rows
Me.DataGridView1.Height += Rows.Height
Next
Me.DataGridView1.Height += Me.DataGridView1.ColumnHeadersHeight
'=====================================================


Note: - (1) Form AutoSize=True
(2) DataGridView ScrollBars=None
(3) DataGridView Dock=None

It will solve your Problem.

Kumar

Nothing is Impossible.....
R.KUMAR  Saturday, August 22, 2009 6:06 AM
Hi Kumar,

Using this method, the form does not expand its width to show all columns (there are four in use during my development and I can currently only see two), and when I attempt to drag the bottom-right of the form to increase the form size, the form will not allow me to perform this action. Presumably because Form AutoSize is set to TRUE.

I would like the form to display to a set height (currently 723 pixels) which means I need to allow use of the vertical scroll bar, but allow its width to alter at run-time depending on the width of the datagridview's data. Ideally, I would like to allow the user to increase the width of the form where the first column is the fill column.

My other option seems to be to calculate the width of all columns and create a function to adjust the width of both the datagridview and the form.

James
James4291  Monday, August 24, 2009 11:17 AM

Hi,

You can set the MinimumWidth property of the column. This will prevent the column too small.

dataGridView1.Columns[0].MinimumWidth = 200;

If you want to resize the form to change the datagridview size, you can change the Anchor property of datagridview,or dock property as @Kumar said.You’d better also set the form minimumsize.

dataGridView1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, August 25, 2009 8:55 AM
Hi James...,

Your datagridview is containing all the columns but since we have set the Datagridview Scrollbars to none, it is happning.
You can set the Scrollbars as you wish. And using this method the form resizes itself because we are setting the width of the form at the load event.

I have written the same code in the Load of Form and its working perfectly.


If you want to resize the form just execute the same code of resizeing the form in Resize Event of Form.

Kumar

Nothing is Impossible.....
R.KUMAR  Tuesday, August 25, 2009 9:00 AM
Hi Ling,

It's the other way around. I want the form to resize once the datagridview has been resized at runtime.

Regards,
James
James4291  Tuesday, August 25, 2009 10:15 AM
Hi Kumar,

The added complication here is that I have a tabcontrol where the first tab is displayed on form_load. The user enters search criteria into this form and the results of which are displayed in the second tab on the tabcontrol. Every time the user enters new search criteria, I want the datagridview to reload.

I have written a sub which is called on each search which gets the longest data item in each column and sets the column width accordingly. The product of all column widths is then used to resize the form.

  Private Sub ResizeDataGridViewAndForm(ByVal myDataGridView As DataGridView, ByVal myForm As Form, Optional ByVal myFillColumn As String = Nothing)

        Dim intSumOfColumnWidths As Integer

        With myDataGridView

            For intColumnLooper As Integer = 0 To .ColumnCount - 1

                ' -- For each visible column ...
                If .Columns(intColumnLooper).Visible = True Then

                    ' -- Set all columns to .AllCells formatting
                    .Columns(myFillColumn).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

                    Dim intColumnWidth As Integer = .Columns(intColumnLooper).Width

                    ' -- For each row ...
                    For intRowLooper As Integer = 0 To .RowCount - 1

                        Dim intWidth As Integer
                        Dim myGraphics As Graphics = Me.CreateGraphics()

                        ' -- Measure the length of the text in the column
                        intWidth = CInt(Math.Round(myGraphics.MeasureString(dgvResults.Item(intColumnLooper, intRowLooper).Value.ToString, _
                        dgvResults.Font).Width, 0))

                        ' -- If DataGridView's column width is less than that necessary for the contained text, set the minimum width
                        If intColumnWidth < CInt(intWidth) Then

                            intColumnWidth = CInt(intWidth)
                            myDataGridView.Columns(intColumnLooper).MinimumWidth = intColumnWidth

                        End If

                    Next

                    ' -- Add column width to running total
                    intSumOfColumnWidths += intColumnWidth

                End If

            Next

            ' -- Set form size
            Dim myFormSize As Size
            myFormSize.Width = intSumOfColumnWidths + Math.Abs(myForm.Width - .Width) ' -- Don't forget to add the padding outside of the DataGridView
            myFormSize.Height = myForm.Height
            myForm.MinimumSize = myFormSize
            myForm.Size = myFormSize

            ' -- Set Fill column
            If Not myFillColumn Is Nothing Then

                .Columns(myFillColumn).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

            End If

        End With

    End Sub
James
James4291  Tuesday, August 25, 2009 10:25 AM

You can use google to search for other answers

Custom Search

More Threads

• Major problem - Help
• DataGrid Error - No Value at Index 2
• Concurrency error with VB2005 and SQL2000
• after doing sorting, was not able to get the sorted rows
• DataGrid TableStyle
• String Value and DataGridView Problem
• Datagridview task-winform-help
• Putting 2 controls into 1 DataGridViewColumn
• To make only the last row of datagridview as editable
• special tooltip for particular cells needed?