?
Solved

Datagridview checkbox event

Posted on 2007-11-21
16
Medium Priority
?
4,843 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

764 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