Solved

Checkbox in DataGridView

Posted on 2006-11-23
10
29,108 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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