如何通过复选框启用和禁用DataGridView中的特定行? [英] How to enable and disable particular row in DataGridView by checkbox?

查看:119
本文介绍了如何通过复选框启用和禁用DataGridView中的特定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过选中和取消选中gridview中的复选框来启用和禁用DataGridView中的特定行。 (C#Windows应用程序)

I am trying to enable and disable specific row in DataGridView by checking and unchecking of checkbox inside gridview. (C# Windows application)

我尝试使用无法按预期工作的CellClick事件。

I tried using the CellClick event which did not work as expected.

这是我尝试过的代码

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0 && dataGridView1.CurrentCell.Selected == true)
    {

        dataGridView1.Columns[3].ReadOnly = false;

    }

}

请告诉我

预先感谢

推荐答案

我认为您错过了 CellContentClick 事件,请尝试以下操作:

I think you missed the CellContentClick event, try this:

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;
          }
    }
}

这篇关于如何通过复选框启用和禁用DataGridView中的特定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆