0
votes

I have a datagridview whose datasource is a datatable. I am adding a checkbox column to this datagridview. Two issues that I am stuck at: 1. When the user clicks on any of the cells in that column, I first need to get the state of the checkbox. 2. If the state is unchecked then a message box pop's up asking if the user wants to check it. This is a YesNoCancel messagebox. The state of the checkbox should turn to checked in the event of the user selecting either Yes or No. For cancel it should leave it to the original state. If the state is checked and the user clicks on the checkbox, a messagebox pop's up asking if the user wants to uncheck it(YesNo), on yes it should change the state to unchecked and on No it shoud leave the checkbox to its original state.

Here is the code that does not seem to work:

if (e.RowIndex != -1)
        {

            DataGridViewRow dr = dataGridView1.CurrentRow;

            DataGridViewCheckBoxCell chkCell = (DataGridViewCheckBoxCell)dr.Cells[0];

            if (chkCell.Value == null)
            {
                DialogResult dResult = MessageBox.Show("Do you want to mark this value for deletion?","Delete",MessageBoxButtons.YesNoCancel);
                if (dResult == DialogResult.Yes)
                {
                    chkCell.Value = true;

                }
                else if (dResult == DialogResult.No)
                {
                    chkCell.Value = true;

                }
                else if (dResult == DialogResult.Cancel)
                {
                    chkCell.Value = false;

                }
            }

            //else if ((bool)chkCell.Value == true)
            else if ((bool)chkCell.Value == true)
            {
               DialogResult dResult = MessageBox.Show("Do you want to unmark this value!","Delete",MessageBoxButtons.YesNo);
               if (dResult==DialogResult.Yes)
                {
                    chkCell.Value = false;
                }
               else
               {
                   chkCell.Value = true;
               }
            }
            else if ((bool)chkCell.Value == false)
            {
                DialogResult dResult = MessageBox.Show("Do you want to mark this value for deletion?", "Delete", MessageBoxButtons.YesNoCancel);
                if (dResult == DialogResult.Yes)
                {
                    chkCell.Value = true;
                }
                else if (dResult == DialogResult.No)
                {
                    chkCell.Value = false;
                }

            }
        }

The code is in the CellContentClick event.

1
Which event is this code located in?Thies
It's in dataGridView1_CellContentClick eventCodeHelp

1 Answers

1
votes

Manage exception for null case, I think it is better.

Then I have rewritten your code with some additions, i.e. EndEdit method of datagridview to commit the original row modification before changing back to previous value.

if (e.RowIndex != -1)
{
    DataGridViewRow dr = dataGridView1.CurrentRow;

    DataGridViewCheckBoxCell chkCell = (DataGridViewCheckBoxCell)dr.Cells[3];

    try 
    {
        if ((bool)chkCell.Value == true)
        {
            DialogResult dResult = MessageBox.Show("Do you want to unmark this value!", "Delete", MessageBoxButtons.YesNo);
            if (dResult == DialogResult.Yes)
            {
                chkCell.Value = false;
            }
            else
            {
                dataGridView1.EndEdit();
                chkCell.Value = true;
            }
        }
        else if ((bool)chkCell.Value == false)
        {
            DialogResult dResult = MessageBox.Show("Do you want to mark this value for deletion?", "Delete", MessageBoxButtons.YesNo);
            if (dResult == DialogResult.Yes)
            {
                chkCell.Value = true;
            }
                else if (dResult == DialogResult.No)
            {
                dataGridView1.EndEdit();
                chkCell.Value = false;
            }
        }
    }
    catch (Exception)
    {
        DialogResult dResult = MessageBox.Show("Do you want to mark this value for deletion?", "Delete", MessageBoxButtons.YesNoCancel);
        if (dResult == DialogResult.Yes)
        {
            chkCell.Value = true;
        }
        else if (dResult == DialogResult.No)
        {
            chkCell.Value = true;
        }
        else if (dResult == DialogResult.Cancel)
        {
            dataGridView1.EndEdit();
            chkCell.Value = false;
        }
    }
}
dataGridView1.EndEdit();