When you make DataGrid Windows Forms C# uneditable, why checkboxes column stops working, help?
Hello, i have a data grid in Windows Forms C#, i have placed a column in it for check boxes, for multi deletion, i have an event on this data grid, if i double click on its row, it opens a popup, which has all the details of this row, the problem is, grid is editable and if we double click, some times it get editing mode and start blinking the cursor for edit, but if i make grid uneditable, the check boxes dont work at all, this is quite stupid, i dont find any function with which i can make the checboxes column to editable and rest grid to uneditable, is it possible to sort out this problem? i dont want grid in editing mode开发者_运维问答 at all, because i have a different form for edit and i cant use it as it has different fields with different types. Attaching a small sample. Thanks in advance.
Another way would be to make the whole datagridview readonly, use the CellMouseClick event and check if the user clicked the cell containing the checkbox and if so toggle the checkbox programmatically:
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (e.ColumnIndex != 2) return; // if column 2 contains checkbox
bool check = (bool)cell.Value;
cell.Value = !check;
}
This is actually easier (in the FormLoad event):
foreach (DataGridViewColumn column in this.dataGridView1.Columns)
{
column.ReadOnly = column.Index == 2 ? false : true;
}
Just set the properties for each column yourself and make everything but the checkbox-column readonly. You can do this directly from the designer. There should be a "Edit Column" command if you click your Grid. The process of setting the columns should be straight-forward. Ask again if you don't find your way.
精彩评论