Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Set the DataGrid View column width dynamically
 

Set the DataGrid View column width dynamically

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.
Progress2007  Tuesday, May 05, 2009 1:36 PM

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.
Linda Liu  Tuesday, May 12, 2009 10:15 AM
Did you try setting the dock property?
mjhillman  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. :)
Han Cheng  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.
Linda Liu  Tuesday, May 12, 2009 10:15 AM

You can use google to search for other answers

Custom Search

More Threads

• how to draw a line on the form?
• Is there a "Fix tab order" mechanism in Visual Studio?
• Customize Progress Bar
• For Next Statement
• Image in treeview
• Incorrect Serialization of Collection property in IExtenderProvider
• Extender Provider
• How can i make a slider menu such as visual studio's toolbox or properties window?
• Refresh problem when using MonthCalendar control in custom UITypeEditor
• Forms.Label overlapping