Solved

Datagridview checkbox event

Posted on 2007-11-21
16
4,839 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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
 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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