如何获取datagridview中的复选框值?

时间:2019-03-21 10:54:43

标签: checkbox

我想检查“ dataGridView1”内部复选框是否被选中。根据复选框的值,我要计算rtn和amt。

int rtn = 0;
int amt=0;
if (str == "GRI")
{
    for (int n = 0; n <= dataGridView1.RowCount - 1; n++)
    {
        rtn = 0;
        amt = 0;
        bool chk =(bool)dataGridView1.Rows[n].Cells[4].Value;
        if (chk)
        {
            dataGridView1.Rows[n].Cells[2].ReadOnly = false;
            rtn +=Convert.ToInt32(dataGridView1.Rows[n].Cells[2].Value);
            amt += Convert.ToInt32(dataGridView1.Rows[n].Cells[2].Value) * Convert.ToInt32(dataGridView1.Rows[n].Cells[3].Value);
        }
        else
        {

        }
    }
}
totpcslbl.Text = rtn.ToString();
totamtrtnlbl.Text = amt.ToString();

1 个答案:

答案 0 :(得分:0)

尝试将其放入Cellclick事件中?

  private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 0 && e.RowIndex >= 0)
        {
            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

            if (chk.Value == chk.TrueValue)
              {
                //your code here
               }
            else
              {
               //do nothing
              }
         }
    }