Solved

Datagridview checkbox event

Posted on 2007-11-21
16
4,824 Views
Last Modified: 2008-07-09
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!
0
Comment
Question by:onebite2
  • 10
  • 6
16 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20329849
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
 

Author Comment

by:onebite2
ID: 20329872
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
 

Author Comment

by:onebite2
ID: 20329883
Can you help me to write the code how should I embed with the check box???????
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20329902

        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
 

Author Comment

by:onebite2
ID: 20329959
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20330003
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
 

Author Comment

by:onebite2
ID: 20330043
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20330074
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:onebite2
ID: 20330116
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
 

Author Comment

by:onebite2
ID: 20330140
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20330213
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
 

Author Comment

by:onebite2
ID: 20330291
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
 

Author Comment

by:onebite2
ID: 20330336
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
 

Author Comment

by:onebite2
ID: 20330443
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 20359125
Do you have the link for the other question (I don't have it anywhere)?

Bob
0
 

Author Comment

by:onebite2
ID: 20500484
I got the problem solved ...Thanks a lot Bob!I appreciate your help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
A simple description of email encryption using a secure portal service. This is one of the choices offered by The Email Laundry for email encryption. The other choices are pdf encryption which creates an encrypted pdf of your email and any attachmen…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now