none
Set the DataGrid View column width dynamically

    Question

  • Hi
    I am using VB.NET 2008.
    I have a dataGridView a and wht i want that when form resizes the column should also be resizes. My probelm is that i have to do it programatically. beacuse grid is containes in a Panel.

    User can resize by two ways, etither by clicking maximize button, or by resizing the form manually at run time.

    Please tell me how can i achieve this.
    Tuesday, May 05, 2009 1:36 PM

Answers

  • Hi Progress2007,

    Even if you dock the Panel to fill the form and dock the DataGridView to fill the Panel, the columns' width in the DataGridView won't resize when the user resize the form. You need to handle the SizeChanged event of the DataGridView to resize the width of the columns in it. For example:

    void dataGridView1_SizeChanged(object sender, EventArgs e)
            {
                for (int i = 0; i < this.dataGridView1.ColumnCount; i++)
                {
                    this.dataGridView1.Columns[i].Width = (this.dataGridView1.Width - this.dataGridView1.RowHeadersWidth)/ this.dataGridView1.ColumnCount;
                }
            }

    Hope this helps.

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    • Marked as answer by Linda Liu Wednesday, May 13, 2009 3:19 AM
    Tuesday, May 12, 2009 10:15 AM

All replies

  • Did you try setting the dock property?
    Sunday, May 10, 2009 12:59 PM
  • You can set the dock property to fill or either play with the anchor property of the grid to the panel. Since your panel also needs to resize upon form resize, you also need to set the dock and anchor property of the panel.

    Try to play around with these two settings. :)
    Tuesday, May 12, 2009 2:39 AM
  • Hi Progress2007,

    Even if you dock the Panel to fill the form and dock the DataGridView to fill the Panel, the columns' width in the DataGridView won't resize when the user resize the form. You need to handle the SizeChanged event of the DataGridView to resize the width of the columns in it. For example:

    void dataGridView1_SizeChanged(object sender, EventArgs e)
            {
                for (int i = 0; i < this.dataGridView1.ColumnCount; i++)
                {
                    this.dataGridView1.Columns[i].Width = (this.dataGridView1.Width - this.dataGridView1.RowHeadersWidth)/ this.dataGridView1.ColumnCount;
                }
            }

    Hope this helps.

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    • Marked as answer by Linda Liu Wednesday, May 13, 2009 3:19 AM
    Tuesday, May 12, 2009 10:15 AM
  •     Private Sub BOMmain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
            For a = 0 To SCMpartnersDataGridView.ColumnCount - 1
                SCMpartnersDataGridView.Columns(a).Width = SCMpartnersDataGridView.Width / SCMpartnersDataGridView.ColumnCount
            Next

            SCMpartnersDataGridView.Refresh()
        End Sub
    Thursday, September 13, 2012 1:31 AM