我试图通过选中和取消选中 gridview 中的复选框来启用和禁用 DataGridView 中的特定行。 (C# Windows 应用程序)
我尝试使用 CellClick 事件,但没有按预期工作。
这是我试过的代码
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && dataGridView1.CurrentCell.Selected == true)
{
dataGridView1.Columns[3].ReadOnly = false;
}
}
请告诉我该怎么做。
提前致谢
最佳答案
我想你错过了 CellContentClick 事件,试试这个:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridView1.Columns["Your Column Name"].Index) //To check that we are in the right column
{
dataGridView1.EndEdit(); //Stop editing of cell.
if ((bool)dataGridView1.Rows[e.RowIndex].Cells["Your Column Name"].Value)
{
//dataGridView1.Columns[3].ReadOnly = true;// for entire column
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
dataGridView1.Rows[colIndex].Cells[rowIndex].ReadOnly = true;
}
}
}
关于c# - 如何通过复选框启用和禁用 DataGridView 中的特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29529887/