Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Checkbox in DataGridView

Posted on 2006-11-23
10
Medium Priority
?
29,143 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 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