Solved

Checkbox in DataGridView

Posted on 2006-11-23
10
29,132 Views
Last Modified: 2011-08-18
Hello!

I got a DataGridView, one column is set to checkbox. I want to grab event when those checkbox are being clicked on. Currently I'm trying to do it via CellContentClick, but I have no idea how to cast from (DataGridViewCellEventArgs^  e) to (DataGridViewCheckboxCell ^t). Of course I do not insist on using this particular event / method, just want to trace checkbox's value changes...

Thanks,
Grzegorz
0
Comment
Question by:GGuzdziol
[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
  • 4
  • 4
  • 2
10 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 18004410
C# code from MSDN samples looks like this:

private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
   DataGridViewCell cell = DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
}

I think that DataGridViewCheckboxCell can be get by casting:

DataGridViewCheckboxCell checkCell = (DataGridViewCheckboxCell)cell;

In C++/CLI this can look like this:

private void DataGridView1_CellContentClick(Object^ sender, DataGridViewCellEventArgs^ e)
{
   DataGridViewCell^ cell = DataGridView1->Rows[e->RowIndex]->Cells[e->ColumnIndex];

   DataGridViewCheckboxCell^ checkCell = (DataGridViewCheckboxCell^)cell;
}
0
 
LVL 14

Author Comment

by:GGuzdziol
ID: 18004614
Yes, that worked...

Sorry but anyway I'm stuck now - how do I determine status (true/false) of this checkbox? I'm really capable of reading, I have MSDN installed...I just cannot find it.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 18006078
bool checked = false;

if ( checkCell != nullPtr  &&  checkCell->Value != nullptr )
{
    checked = (bool)checkCell->Value;
}
0
[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

 
LVL 14

Author Comment

by:GGuzdziol
ID: 18011621
I also think this should work...but it simply doesn't!

My current code is:

private: System::Void clc(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
                   if(e->ColumnIndex == 0) {
                         DataGridViewCell^ cell = this->dataGridView1->Rows[e->RowIndex]->Cells[e->ColumnIndex];

                         DataGridViewCheckBoxCell^ checkCell = (DataGridViewCheckBoxCell^)cell;
                         bool checked = false;

                         if((checkCell != nullptr)&&(checkCell->Value != nullptr)) {
                               checked = (bool)checkCell->Value;
                         }
                         if(checked) {
                               MessageBox::Show("yeah!");
                         }
                   }
             }

The behaviour is very strange for me. Sometimes it always shows messagebox, sometimes never. I guess that it is bad event that I put this code on (CellContentClick) but msdn says:
,,If you want to respond immediately when users click a check box cell, you can handle the DataGridView.CellContentClick event, but this event occurs before the cell value is updated. If you need the new value at the time of the click, one option is to calculate what the expected value will be based on the current value''
I don't understand.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 18011722
This means, this handler is called before value is changed. If checked value is false, new value will be true. If checked value is true, it will be false.
If you want to know new state, it is better to handle DataGridView.CellValueChanged event.
0
 
LVL 14

Author Comment

by:GGuzdziol
ID: 18011778
If it worked like You say (and as it's written in msdn) I could just negate this value and voila. But it doesn't...I'll take a look at CellValueChanged.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 18011827
BTW, you can get much more information about DataGridView in C# or VB .NET area. Such things are usually written in C# or VB. C++/CLI is used mostly for interoperability.
If you have some reasons to do this stuff in C++/CLI, you can ask syntax questions here, and general questions about database, controls like DataGridView in other .NET areas. C++/CLI developers don't work with this.
0
 
LVL 14

Author Comment

by:GGuzdziol
ID: 18021178
It was enough to take care about CurrentCellDirtyStateChanged while using CellValueChanged. Thanks.
0
 

Expert Comment

by:jpawar
ID: 23260783
DataGridView dgv = sender as DataGridView;
            DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
            bool isChecked = (bool)cell.EditedFormattedValue;

            if (isChecked)
            {
                //Checkbox is being checked                
            }
            else
            {
                //Checkbox is being unchecked
            }
0
 

Expert Comment

by:jpawar
ID: 23260786
Updated Comment
***********************
private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
            DataGridView dgv = sender as DataGridView;
            DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
            bool isChecked = (bool)cell.EditedFormattedValue;

            if (isChecked)
            {
                //Checkbox is being checked                                
            }
            else
            {
                //Checkbox is being unchecked
            }
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

627 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