Triggering a checkbox value changed event in DataGridView

前端 未结 15 2879
一生所求
一生所求 2020-12-01 08:02

I have a grid view that has a check box column, and I want to trigger a drawing event as soon as the value of the cell is toggled. I tried the ValueChaged and the CellEndEdi

15条回答
  •  攒了一身酷
    2020-12-01 08:24

    Use this code, when you want to use the checkedChanged event in DataGrid View:

    private void grdBill_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        grdBill.CurrentCell =  grdBill.Rows[grdBill.CurrentRow.Index].Cells["gBillNumber"];
    }
    
    private void grdBill_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        calcBill();
    }
    
    private void calcBill()
    {
        listBox1.Items.Clear();
        for (int i = 0; i < grdBill.Rows.Count - 1; i++)
        {
            if (Convert.ToBoolean(grdBill.Rows[i].Cells["gCheck"].Value) == true)
            {
                listBox1.Items.Add(grdBill.Rows[i].Cells["gBillNumber"].Value.ToString());
            }
        }
    }
    

    Here, grdBill = DataGridView1, gCheck = CheckBox in GridView(First Column), gBillNumber = TextBox in Grid (Second column).

    So, when we want to fire checkchanged event for each click, first do the CellContentClick it will get fire when user clicked the Text box, then it will move the current cell to next column, so the CellEndEdit column will get fire, it will check the whether the checkbox is checked and add the "gBillNumber" in list box (in function calcBill).

提交回复
热议问题