Datagridview checkbox event

I have datagridview (winforms )control with a collection of columns bound to it.This datagrid view when clciked on any column in the row displays a list of information for that particular user like Billing Info,creditc ard info.
I have a check box column called "Confirm" in the grid.I need the check box to show a dialog box with a message asking for confirmation
 when the user puts a check mark on it and when the user removes the check mark in the check box.
My initial step of code was:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.RowIndex>-1)&& dataGridView1.Columns[5]is DataGridViewCheckBoxColumn)
            {
                MessageBox.Show("sure");
            }
        }


Need help with the code.

Thanks in advance!
onebite2Asked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
Do you have the link for the other question (I don't have it anywhere)?

Bob
0
 
Bob LearnedCommented:
I like to bind an event handler to the CheckBox control from the EditingControlShowing event handler.

Here is an example for the KeyPress event:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
   dataGridView1.EditingControl.KeyPress -= new KeyPressEventHandler(EditingControl_KeyPress);

   dataGridView1.EditingControl.KeyPress += new KeyPressEventHandler(EditingControl_KeyPress);
}

Bob
0
 
onebite2Author Commented:
Hey Bob,

So do I have to add an other event handler form the one I wrote or how do I embed that????

Please help me!
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
onebite2Author Commented:
Can you help me to write the code how should I embed with the check box???????
0
 
Bob LearnedCommented:

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            CheckBox chk = e.Control as CheckBox;
 
            if (chk != null)
            {
                chk.CheckedChanged -= new EventHandler(OnCheckBoxChanged);
                chk.CheckedChanged += new EventHandler(OnCheckBoxChanged);
            }
        }
 
        private void OnCheckBoxChanged(object sender, EventArgs e)
        {
        }

Open in new window

0
 
onebite2Author Commented:
Hey Bob,
I embedded the same code it should show a dialog box but this is not working....can you give me an idea as to how to develop this so that when a user clicks the checkbox a dialog box appears asking for the confirmation ????



Here goes the code:


private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            CheckBox chk = e.Control as CheckBox;

            if (chk != null)
            {
                chk.CheckedChanged -= new EventHandler(OnCheckBoxChanged);
                chk.CheckedChanged += new EventHandler(OnCheckBoxChanged);
            }
        }

        private void OnCheckBoxChanged(object sender, EventArgs e)
        {

            if (dataGridView1.Columns[5] is DataGridViewCheckBoxColumn)
            {
                MessageBox.Show("sure");
            }
        }
0
 
Bob LearnedCommented:
Something like this might be what you are looking for:
        private void OnCheckBoxChanged(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                CheckBox chk = sender as CheckBox;
                if (chk != null)
                {
                    // Restore the previous value.
                    chk.Checked = !chk.Checked;
                }
            }
        }        

Open in new window

0
 
onebite2Author Commented:
Hey Bob,

I did use this code but there were errors like:

Error:The best overloaded method match for 'System.Windows.Forms.MessageBox.Show(string, string, System.Windows.Forms.MessageBoxButtons)'

Error: cannot convert from 'System.Windows.Forms.MessageBoxButtons' to 'string'.

Waht should I do now ??????

Thanks!      

0
 
Bob LearnedCommented:
Sorry, I didn't compile :(

            if (MessageBox.Show("Are you sure?", "Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)

You need match the correct signature for the 21 overloaded methods for MessageBox.show.  I added the "Caption" string, which is the window title for the message box.

Bob
0
 
onebite2Author Commented:
Thanks Bob!

But where do I have to include that particular column in this code so that when the user clicks on that column then the dialog box is shown?????

for example the column would be in :dataGridView1.Columns[5]

Is that enough?????or do I have to write  a separate code for that particular column?
0
 
onebite2Author Commented:
or do I have to include this line of sentence which says that column in datagrid is the place where this event is fired..????????

dataGridView1.Columns[5]is DataGridViewCheckBoxColumn


Please help me!

Thanks!
0
 
Bob LearnedCommented:
Hmmm, that is a different story.  Let's go back to the beginning...

Did this work when you clicked the check box?

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.RowIndex>-1)&& dataGridView1.Columns[5]is DataGridViewCheckBoxColumn)
            {
                MessageBox.Show("sure");
            }
        }

Bob
0
 
onebite2Author Commented:
No it did not work......This whole datagrid view is in agroup box  so when the user enters a name and clciks ona button code iterates through all th columns and gets into this groupbox and from there into an other event dataGridView1_CellContentClick_1....so the code should be placed in the cell contentclcik1 as far as I know to make the chechbox work .....


        private void groupBox5_Enter(object sender, EventArgs e)
        {
           
        }

 private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
           
       
        }
0
 
onebite2Author Commented:
So I placed the code in this but it is not getting into the loop it is not  reading the if condition......

private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.RowIndex>-1)&& dataGridView1.Columns[5]is DataGridViewCheckBoxColumn)
            {
                MessageBox.Show("sure");
            }
        }
0
 
onebite2Author Commented:
Hey Bob,

This is cindy.You gave me a solution yesterday for populating the listbox the final output is still not settled can you help me with that too .
Here goes my problem:

Instead of showing the product Name in the list box and product ID it is displaying the :method name as console.userdata+productsRegistered.

What should I do inorder look at the product name on the screen.The name is read in the code  but not on th list box.


Please help me with this also!I need some help

Thanks in advance!
0
 
onebite2Author Commented:
I got the problem solved ...Thanks a lot Bob!I appreciate your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.