Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

DataGridView checkbox column value (NET Framework 4)

$
0
0

I have a datagridview with a checkbox clomunn. When checkbox is checked/unchecked the messagebox appears with question "Do you really want to change a value?" If Yes is clicked the checkbox value set to a new value. If No button is clicked the checkbox value return to old value.
How can i do that ? This is my code but it doesn't work. When I clikced No button the value return, but when current cell losts focus the value is changed again to a new value.

        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView1.IsCurrentCellDirty)
            {
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
                return;

            if (e.ColumnIndex == dataGridView1.Columns["checkboxcol"].Index)
            {
                DataGridViewCheckBoxCell dgvc = (DataGridViewCheckBoxCell)dataGridView1.Rows[e.RowIndex].Cells["checkboxcol"];

                DialogResult mbDr = MessageBox.Show("Question","AppName",
                                                     MessageBoxButtons.YesNo,
                                                     MessageBoxIcon.Question,
                                                     MessageBoxDefaultButton.Button2);

                if (mbDr == System.Windows.Forms.DialogResult.No)
                {
                    dataGridView1.CancelEdit();
                }
            }
        }



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>