none
Checkbox in datagridview CellContentClick event

    Question

  • I have a checkbox column in my datagridview.  But when I click the checkbox the cellContentClick event is raised.  But when I try and see if that checkbox is checked it still says that it isn't until after the event finishes then I can see it's checked.  How can I get around this issue?


    1 for (x = 0; x < dgv.RowCount; x++)  
    2       {  
    3 DataGridViewCheckBoxCell chkactive = (DataGridViewCheckBoxCell)dgv.Rows[x].Cells[0];  
    4  
    5         if (chkactive.Value != null)  
    6         {  
    7           int TableId = Convert.ToInt32(dgv.Rows[x].Cells[1].Value);  
    8         }  
    9  
    10       } 


    Any help would be greatly appreciated.

    Thank You
    Wednesday, March 18, 2009 3:33 PM

Answers

  • Hi Jerry8989,

    DataGridView.CellContentClick Event occurs when the content within a cell is clicked.

    For clicks in a DataGridViewCheckBoxCell, this event occurs before the check box changes value, so if you do not want to calculate the expected value based on the current value, you will typically handle the DataGridView.CellValueChanged event instead. Because that event occurs only when the user-specified value is committed, which typically occurs when focus leaves the cell, you must also handle the DataGridView.CurrentCellDirtyStateChanged event. In that handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.

    I don't very clearly know what is your purpose. So please follow up with necessary information if you still have problem. Thank you!

    Best regards,
    Guo

    • Proposed as answer by Guo Surfer Monday, March 23, 2009 3:18 AM
    • Marked as answer by Guo Surfer Wednesday, March 25, 2009 3:14 AM
    Friday, March 20, 2009 12:20 PM
  • One quick and dirty method is ending the edit

     dgv.EndEdit();
    for (x = 0; x < dgv.RowCount; x++)  
    .................................
    .......................

    Arjun Paudel
    • Proposed as answer by Guo Surfer Monday, March 23, 2009 3:18 AM
    • Marked as answer by Guo Surfer Wednesday, March 25, 2009 3:14 AM
    Friday, March 20, 2009 12:34 PM

All replies

  • Hi Jerry8989,

    DataGridView.CellContentClick Event occurs when the content within a cell is clicked.

    For clicks in a DataGridViewCheckBoxCell, this event occurs before the check box changes value, so if you do not want to calculate the expected value based on the current value, you will typically handle the DataGridView.CellValueChanged event instead. Because that event occurs only when the user-specified value is committed, which typically occurs when focus leaves the cell, you must also handle the DataGridView.CurrentCellDirtyStateChanged event. In that handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.

    I don't very clearly know what is your purpose. So please follow up with necessary information if you still have problem. Thank you!

    Best regards,
    Guo

    • Proposed as answer by Guo Surfer Monday, March 23, 2009 3:18 AM
    • Marked as answer by Guo Surfer Wednesday, March 25, 2009 3:14 AM
    Friday, March 20, 2009 12:20 PM
  • One quick and dirty method is ending the edit

     dgv.EndEdit();
    for (x = 0; x < dgv.RowCount; x++)  
    .................................
    .......................

    Arjun Paudel
    • Proposed as answer by Guo Surfer Monday, March 23, 2009 3:18 AM
    • Marked as answer by Guo Surfer Wednesday, March 25, 2009 3:14 AM
    Friday, March 20, 2009 12:34 PM
  •  Guo & Arjun,
    Thank you both for your replies.

    What i'm doing is when the user clicks the checkbox the  DataGridView_CellContentClick event fires within that event I set focus on the DataGridView.

    DataGridView.Focus();
    I did this so I can leave the clicked cell and have DataGridView_CellValueChanged fire.  This is where I have most of my code.

    Is this method ok or am I not doing it the proper way?


    I might end up using a 3rd party control since the DataGridView refreshes so slow even with 50 rows of data.  I even tested with populating the DataGridView with the numbers 1-50 and when I scroll it's choppy.


    Thanks again guys

    Jerry
    Friday, March 20, 2009 1:03 PM
  • Hi Jerry,

    Thank you for you reply.
    Have you resolved your problem?  I'm sorry that I still don't very well understand your purpose. Would you mind providing more necessary information about your purpose if you still have problem? Please check the related events' links I have mentioned above.
    Thank you!

    Best regards,
    Guo
    Monday, March 23, 2009 3:18 AM
  • Guo,
    I haven't solved that problem yet.  I'm trying to do it with a 3rd party control because the datagridview is very choppy when I scroll a list of a 100 and I can't have that when I release this.  The 3rd party control does the onchange event as you mentioned above and it works fine.

    The main purpose of this app was to allow the user to select from 3 different combo boxes.  Based on the selections a datagridview is populated.  When the user changes the value of a checkbox that specific rows data will populate or unpopulate a different datagridview and from there the user can click on another checkbox to populate a 3rd datagridview with more data. 

    There are a lot of ways this data changes and it's frustrating that the datagridview was slow and the re-painting when I scrolled was so bad.  I even populated it with the numbers 1-100 and scrolled through it and it was still choppy.
    If you can shed any light on that I would appreciate it just in case the 3rd party control doesn't work. 


    Thank you so much for you help it is appreciated.

    Jerry
    Monday, March 23, 2009 1:16 PM
  • Hi Jerry,

    Thank you for you response.
    It should be to say that datagridview has no problem when scroll with only 100 records. The choppy problem as you mentioned may has something to do with your code populating the datagridview, so would you mind providing your sample code populating the datagridview if you use DataGridView Control.
    Please consider posting your thread to third-party control related forums if you use them. Thank you for your understanding!

    Best regards,
    Guo
    Tuesday, March 24, 2009 3:09 AM
  • Guo,
    The code at the bottom of this thread

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/e4c73245-107e-40eb-a61d-d0e3d0453095/

    Causes my grid to be choppy.  That is how I populated it for my test and it was still slow to paint.

    Sorry for the dup threads wasn't my intention.


    Thank You
    Jerry
    Tuesday, March 24, 2009 12:54 PM
  • Hi Jerry,

    Thank you for your response.
    I have said in that thread, your sample code populating datagridview has no problem. The choppy problem may has something to do with other code in your project. Would you mind sending your project to my email, t-guosur at microsoft dot com.  Or please consider posting your thread to Windows Forms Data Controls and Databinding Forum. You may get quicker and better responses there.
    Thank you!

    Best regards,
    Guo
    • Edited by Guo Surfer Wednesday, March 25, 2009 3:19 AM
    Wednesday, March 25, 2009 3:07 AM
  • Hi Jerry,

    We are marking the above posts as answers for your first question "Checkbox in datagridview CellContentClick event".
    For your another question "datagridview choppy problem", I'm very sorry I can't figure out right now, please consider posting your thread to Windows Forms Data Controls and Databinding Forum. You may get quicker and better responses there.
    Thank you very much for your understanding!

    Best regards,
    Guo
    Wednesday, March 25, 2009 3:17 AM
  • Guo,
    Thank you for all your help. 
    That link you posted says "HTTP 403 Forbidden" is that the correct link?
    Since you are busy I will just post my info there and not bother you with the project.

    Here is the entire code for that form with the datagridview that is choppy.

    using System;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.Linq;  
    using System.Text;  
    using System.Windows.Forms;  
     
    namespace WinFormCheckBoxTest  
    {  
      public partial class Form3 : Form  
      {  
        public Form3()  
        {  
          InitializeComponent();  
        }  
     
        private void Form3_Load(object sender, EventArgs e)  
        {  
          DataTable dt = new DataTable();  
     
          dt.Columns.Add("Num");  
            
          int i=0;  
          while (i <= 100)  
          {  
            dt.Rows.Add();  
            dt.Rows[i][0] = i;  
            i++;  
          }  
          dataGridView1.DataSource = dt;  
        }  
      }  
    }  
     
    Wednesday, March 25, 2009 1:45 PM