Solved

Checkbox in DataGridView

Posted on 2006-11-23
10
29,125 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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