Solved

Datagridview checkbox event

Posted on 2007-11-21
16
4,821 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
Comment Utility
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
Comment Utility
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
Comment Utility
Can you help me to write the code how should I embed with the check box???????
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility

        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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:onebite2
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Do you have the link for the other question (I don't have it anywhere)?

Bob
0
 

Author Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

14 Experts available now in Live!

Get 1:1 Help Now