DataGridViewCheckBoxColumn CellMouseClick

时间:2013-01-28 07:17:52

标签: c# winforms datagridview

我使用的DataGridView包含CheckBox列。当我尝试检索该列的值时,它始终为false。请让我知道原因。

这是我的代码:

private void dataGridViewCrossRef_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
     {
       bool isChecked1 = false;
       isChecked1 = (Boolean)dataGridViewCrossRef[25, e.RowIndex].FormattedValue;
       if (isChecked1)
       {
          //Some code
       }
     } 

2 个答案:

答案 0 :(得分:0)

尝试CellContentClick Event

    private void dataGridViewCrossRef_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 25)
        {
            bool IsBool = false;
            if (bool.TryParse(dataGridViewCrossRef[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString(), out IsBool))
            {
               //Some code
            }
        }
    }

修改

CellClick Event中尝试此操作,您需要将dataTable声明为public

   private void dataGridViewCrossRef_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 25)
        {
            //Find primaykey or something unique from your dataTable   
            DataRow[] Rows = dataTable.Select("Id = '" + dataGridViewCrossRef[0, e.RowIndex].EditedFormattedValue.ToString() + "'");

            Rows[0]["NameOfColumnHasCheckBox"] = !bool.Parse(Rows[0]["NameOfColumnHasCheckBox"].ToString());
        }
    }

答案 1 :(得分:0)

由于数据绑定到Chekbox是整数所以我已经将代码更改为低于它并且它起作用。

由于

private void dataGridViewCrossRef_CellContentClick(object sender,DataGridViewCellEventArgs e)         {             if(e.ColumnIndex == 25)             {                 int CellValue = 0;                 CellValue = Convert.ToInt16(dataGridViewCrossRef [e.ColumnIndex,e.RowIndex] .EditedFormattedValue);                 if(CellValue == 1)                 {

            }
        }
    }