Solved

Checkbox in DataGridView

Posted on 2006-11-23
10
29,094 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Please suggest any one for my question.... 4 98
VB Email Check 2 244
_itoa_s  equivalent in c# 3 141
c++ open source accounting software 3 85
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now