Triggering a checkbox value changed event in DataGridView

前端 未结 15 2777
一生所求
一生所求 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:17

    Another way is to handle the CellContentClick event (which doesn't give you the current value in the cell's Value property), call grid.CommitEdit(DataGridViewDataErrorContexts.Commit) to update the value which in turn will fire CellValueChanged where you can then get the actual (i.e. correct) DataGridViewCheckBoxColumn value.

    private void grid_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
       grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
    
    private void grid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        // do something with grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value
    }
    

    Target .NET framework: 2.0

    0 讨论(0)
  • 2020-12-01 08:18

    I finally implemented it this way

      private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
    
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                if (dataGridView1[e.ColumnIndex, e.RowIndex].GetContentBounds(e.RowIndex).Contains(e.Location))
                {
                    cellEndEditTimer.Start();
                }
            }
    
        }
    
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        { /*place your code here*/}
    
    
        private void cellEndEditTimer_Tick(object sender, EventArgs e)
        {
            dataGridView1.EndEdit();
            cellEndEditTimer.Stop();
        }
    
    0 讨论(0)
  • 2020-12-01 08:23

    Every one of the CellClick and CellMouseClick answers is wrong, because you can change the value of the cell with the keyboard and the event will not fire. Additionally, CurrentCellDirtyStateChanged only fires once, which means if you check/uncheck the same box multiple times, you will only get one event. Combining a few of the answers above gives the following simple solution:

    private void dgvList_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (dgvList.CurrentCell is DataGridViewCheckBoxCell)
            dgvList.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
    
    private void dgvList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        // Now this will fire immediately when a check box cell is changed,
        // regardless of whether the user uses the mouse, keyboard, or touchscreen.
        //
        // Value property is up to date, you DO NOT need EditedFormattedValue here.
    }
    
    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-12-01 08:26

    Using the .EditedFormattedValue property solves the problem

    To be notified each time a checkbox in a cell toggles a value when clicked, you can use the CellContentClick event and access the preliminary cell value .EditedFormattedValue.

    As the event is fired the .EditedFormattedValue is not yet applied visually to the checkbox and not yet committed to the .Value property.

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
       var checkbox = dataGridView1.CurrentCell as DataGridViewCheckBoxCell;
    
       bool isChecked = (bool)checkbox.EditedFormattedValue;
    
    }
    

    The event fires on each Click and the .EditedFormattedValue toggles

    0 讨论(0)
  • 2020-12-01 08:29

    "EditingControlShowing" event doesn't fire on checkbox value change. Because display style of the checkbox cell doesn't not change.

    The workaround i have used is as below. (I have used CellContentClick event)

        private void gGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (string.Compare(gGridView1.CurrentCell.OwningColumn.Name, "CheckBoxColumn") == 0)
            {
                bool checkBoxStatus = Convert.ToBoolean(gGridView1.CurrentCell.EditedFormattedValue);
                //checkBoxStatus gives you whether checkbox cell value of selected row for the
                //"CheckBoxColumn" column value is checked or not. 
                if(checkBoxStatus)
                {
                    //write your code
                }
                else
                {
                   //write your code
                }
            }
        }
    

    The above has worked for me. Please let me know if need more help.

    0 讨论(0)
提交回复
热议问题