locked
datagridview with checkbox column RRS feed

  • Question

  • i have a datagridview with checkbox column
    i want to know the method that when i directly on check or uncheck the checkbox cell

    to do my code

    i wrote this code but it's didnt work

     

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

    {

     

    if (e.ColumnIndex == 0)

    {

     

    if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[0].Value) == false)

    {

     

    MessageBox.Show("you uncheck the row ="+e.RowIndex.ToString());

    }

    }

    }

    can any on help me

    Saturday, September 24, 2011 9:04 AM

Answers

  • Saturday, September 24, 2011 9:26 AM
  • Hi, make sure the cell *has* a value and ...

    ...(this will report the current state of the checkbox, so it will report when you change the state of the checkBox)

     

            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                //maybe use a different ErrorContext
                if (e.RowIndex > -1 && this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                    MessageBox.Show(((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).GetEditedFormattedValue(e.RowIndex, DataGridViewDataErrorContexts.Formatting).ToString());
            }
    

    Regards,

      Thorsten


    Saturday, September 24, 2011 9:27 AM
  • ... respectively (works also if the value previously was null):

            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                //maybe use a different ErrorContext
                if (e.RowIndex > -1)
                    MessageBox.Show(((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).GetEditedFormattedValue(e.RowIndex, DataGridViewDataErrorContexts.Formatting).ToString());
            }
    

    Regards,

      Thorsten

    Saturday, September 24, 2011 9:34 AM
  • To get the current state of the clicked checkBox, you will have to subscribe to two (2) event:

    - CurrentCellDirtyStateChanged

    - CellValueChanged

     

    This is then the code which will get the current state of clicked checkedBox:

            void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
            {
                if (dataGridView1.IsCurrentCellDirty)
                    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
    
            private void dataGridView1_CellValueChanged(object obj, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 0) //compare to checkBox column index
                {
                    DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
                    if (!DBNull.Value.Equals(cbx.Value) && (bool)cbx.Value == true)
                    {                 
                        //checkBox is checked - do the code in here (show a message as you did in your example!
                    }
                    else
                    {
                       //if checkBox is NOT checked (unchecked)
                    }
                }
            }
    


     


    Mitja
    Saturday, September 24, 2011 9:52 AM

All replies

  • Saturday, September 24, 2011 9:26 AM
  • Hi, make sure the cell *has* a value and ...

    ...(this will report the current state of the checkbox, so it will report when you change the state of the checkBox)

     

            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                //maybe use a different ErrorContext
                if (e.RowIndex > -1 && this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                    MessageBox.Show(((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).GetEditedFormattedValue(e.RowIndex, DataGridViewDataErrorContexts.Formatting).ToString());
            }
    

    Regards,

      Thorsten


    Saturday, September 24, 2011 9:27 AM
  • ... respectively (works also if the value previously was null):

            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                //maybe use a different ErrorContext
                if (e.RowIndex > -1)
                    MessageBox.Show(((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).GetEditedFormattedValue(e.RowIndex, DataGridViewDataErrorContexts.Formatting).ToString());
            }
    

    Regards,

      Thorsten

    Saturday, September 24, 2011 9:34 AM
  • To get the current state of the clicked checkBox, you will have to subscribe to two (2) event:

    - CurrentCellDirtyStateChanged

    - CellValueChanged

     

    This is then the code which will get the current state of clicked checkedBox:

            void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
            {
                if (dataGridView1.IsCurrentCellDirty)
                    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
    
            private void dataGridView1_CellValueChanged(object obj, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 0) //compare to checkBox column index
                {
                    DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
                    if (!DBNull.Value.Equals(cbx.Value) && (bool)cbx.Value == true)
                    {                 
                        //checkBox is checked - do the code in here (show a message as you did in your example!
                    }
                    else
                    {
                       //if checkBox is NOT checked (unchecked)
                    }
                }
            }
    


     


    Mitja
    Saturday, September 24, 2011 9:52 AM