Set the DataGrid View column width dynamically
-
Tuesday, May 05, 2009 1:36 PMHi
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.
All Replies
-
Sunday, May 10, 2009 12:59 PMDid you try setting the dock property?
-
Tuesday, May 12, 2009 2:39 AMYou 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 10:15 AMModerator
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 LiuModerator Wednesday, May 13, 2009 3:19 AM
-
Thursday, September 13, 2012 1:31 AMPrivate 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


