I have a column that holds Checkboxes.
My main problem is that when I call this function:
dataGrid.AutoResizeColumn(0, DataGridViewAutoSizeColumnMode.DisplayedCells);
It stretches the checkbox column too, and I want that column to stay in 25 in width.
How can I do that? (only checkbox column not stretched)
Here is some more code, showing what I开发者_如何学运维 want to happen:
dataGrid.AutoResizeColumn(0, DataGridViewAutoSizeColumnMode.DisplayedCells);
dataGrid.AutoResizeColumn(1, DataGridViewAutoSizeColumnMode.Fill);
dataGrid.AutoResizeColumn(2, DataGridViewAutoSizeColumnMode.Fill);
dataGrid.AutoResizeColumn(3, DataGridViewAutoSizeColumnMode.DisplayedCells);
dataGrid.AutoResizeColumn(4, DataGridViewAutoSizeColumnMode.Fill);
dataGrid.AutoResizeColumn(5, DataGridViewAutoSizeColumnMode.Fill);
But the fill gives me an error.
What you are asking doesn't quite make sense, since the method you show AutoResizeColumn take as its first parameter the column index to resize - if you call this method with the index of the checkbox column then your are explicitly telling the grid to resize that column. If you don't want the resize, don't do that!
If you set the resize mode the next level up for the grid, you do it like this:
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
Now with this method of setting the sizing mode then yes, the checkbox will change when you might want it to.
The answer to that is to use either the designer or the method you mention above, and set the checkbox columns autosize mode to None
If you absolutely must loop over the columns setting their AutoResize mode, then the only option you have is to check if you have the checkbox column in the loop and apply a different more.
This is in answer to your comment - the error you will be seeing is an ArgumentException being thrown by the AutoResizeColumn method. This is all documented on the MSDN page for the AutoResizeColumn method. You cannot specify a AutoSize mode of None or Fill.
It sounds like what you want to do is something like:
// If column 3 is the checkbox column, we set its resize mode to none:
dataGridView1.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
// Then we set the width:
dataGridView1.Columns[3].Width = 25;
// Finally we set the rest of the grid to fill or what ever resizing you need:
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
you can define in datagridview_cellpainting event
private void gvDocumentList_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == gvDocumentList.Columns["checkbox column name"].Index && e.RowIndex >= 0)
{
e.PaintBackground(e.ClipBounds, true);
Rectangle rectRadioButton = new Rectangle();
rectRadioButton.Width = 14;
rectRadioButton.Height = 14;
rectRadioButton.X = e.CellBounds.X + (e.CellBounds.Width - rectRadioButton.Width) / 2;
rectRadioButton.Y = e.CellBounds.Y + (e.CellBounds.Height - rectRadioButton.Height) / 2;
e.Paint(e.ClipBounds, DataGridViewPaintParts.Focus);
e.Handled = true;
}
}
精彩评论