.NET VS2010 DataGridView AutoSizeColumnsMode setting prohibits user resizing
I'm having a weird issue in a program I'm developing for .NET 3.5 using C# in Visual Studio 2010.
I'm populating a DataGridView with information and I'd like to resize the data grid columns according to the headers, but also allow the user to manually resize the colums if the data is longer than the length of the header value.
So I changed my DataGridView.AutoSizeColumnsMode = ColumnHeader. The DataGridView is now resizing the columns automatically according to the header, but suddenly I find that I can't resize the columns manually with my mouse even though my DataGridView.AllowUserToResizeColumns property is true.
My question is how can I automatically size my data grid view columns while still allowing the user to r开发者_Go百科esize the columns?
I was racking my head on the same issue (more or less) and I couldn't figure it out.
I made sure that the following properties were set
AllowUserToResizeColumns = True
AutoSizeColumnsMode = None
Then in my code, I set the following after I filled my grid with data:
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
The OP would obviously instead use:
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader)
That should have done it, so you auto re-sized the Columns once, but otherwise the AutoSizeColumnsMode is set to None, so the user still has control.
...My problem was that I forgot that the Columns themselves have properties that need to be taken into account as well. I had set most of the columns to have:
AutoSizeMode = AllCells
which was the source of my problem...so I set them to
AutoSizeMode = NotSet
and all is as it should be.
The solution from here coverted to C# and tested:
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
// Size the column header based on the ColumnHeader mode
dataGridView1.Columns[i].AutoSizeMode =
DataGridViewAutoSizeColumnMode.ColumnHeader;
// Store autosized width
int colw = dataGridView1.Columns[i].HeaderCell.PreferredSize.Width;
// Change back to Resize mode
dataGridView1.Columns[i].AutoSizeMode =
DataGridViewAutoSizeColumnMode.None;
// Set width to calculated above
dataGridView1.Columns[i].Width = colw;
}
Just found a better solution from MSDN. Before the DataGridView is displayed, use the AutoResizeColumn
function. For example:
void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
dataGridView1.AutoResizeColumn(
i, DataGridViewAutoSizeColumnMode.ColumnHeader);
}
}
The way i do it, is more simple:
First fill your grid with the desired data. Then, set the following properties (you can also set these in the designer of course)
datagridResult.DataSource = YourDataSourceHere
datagridResult.AllowUserToOrderColumns = true;
datagridResult.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
Now, Add the following code, in the eventhandler "Shown" of the form:
private void MainForm_Shown(object sender, EventArgs e)
{
Application.DoEvents();
datagridResult.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
}
That's all :)
Use the solution from here:
How do you automatically resize columns in a DataGridView control AND allow the user to resize the columns on that same grid
And instead of using
grd.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
Use:
grd.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader
detailsDataGridView.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells doesn't work?
Found it here.
If that doesn't, you can always manually handle it and just resize the column whenever a cell's data changes to be equal to the largest cell.
The code should be like this , hope this will helpful
datagridView1.Columns[columnindex].AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
精彩评论