DatagridView Checkbox Checked?

前端 未结 2 1671
夕颜
夕颜 2021-01-24 10:35

i have a check box in a datagridview windows form and have a event handler cell_Click on cell click i check the datagridview column for a check box it shows true if the cell is

相关标签:
2条回答
  • 2021-01-24 11:06

    If I understand you correctly you are saying the checkbox value does not align with the underlying data?

    This may well be because the data has been updated and is 'dirty', e.g. it hasn't been committed to the datasource yet. If you add an event handler like this:

    private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
       if (dataGridView1.CurrentCell is System.Windows.Forms.DataGridViewCheckBoxCell)
       {
          dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
       }
    }
    

    Then that should update the datasource and you'll have the correct checkbox state when you query the cell.

    0 讨论(0)
  • 2021-01-24 11:17

    Several things here:

    • the cell click event just means that the user clicked with the mouse button on the data grid view, what you're looking for is probably the CellValueChanged
    • this event will give you the coordinates of the cell that changed. You should check to see if it's in your check box column, then get a reference to the cell and you can check the cell.Value to see if it's true or false. You're not going to find any values on the DataGridViewCheckBoxColumn -- it's going to be at the cell level, and you'll always find the value stored in cell.Value, no matter what type of column it is.
    0 讨论(0)
提交回复
热议问题