c# - 如何通过复选框启用和禁用 DataGridView 中的特定行?

标签 c# winforms datagridview

我试图通过选中和取消选中 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/

相关文章:

c# - 将 .NET Core 与旧版 .NET 框架 dll 一起使用

c# - 通过Winforms应用程序和PCI合规性调用Paypal

c# - 通过进程名称取消隐藏进程?

vb.net - 对字母数字数据 View 列进行排序

c# - 为什么不更新绑定(bind)的 DataGridView 单元格?

vb.net 将 datagridview 列类型更改为组合框

c# - 在任务栏中最小化表单时将其置于最前面

c# - 我可以创建一个网络服务来流式传输 pdf 以供下载吗

c# - C#中如何读取文件的特定行

c# - 将自定义控件替换为现有控件