Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
Hi all;
 
I have a Windows form with a DataGridView control, and there are several columns of type DataGridViewCheckBoxColumn that I want to read (checked or unchecked). Everything is unbounded.
 
Now this of course should be the simplest thing, but something isn't working as it should. For example, I have a column called "Options", and I want to see if a particular row's "Options" checkbox is checked or not. Just for fun, read the value and display it in a message box:
 
I've tried
if (dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[dataGridView.Columns["Options"].Index].Value  != null)
    {
        // Do some stuff
        MessageBox.Show(dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[dataGridView.Columns["Options"].Index].Value.ToString());
    }
 
and I've also tried
 
DataGridViewCheckBoxCell cbox = new DataGridViewCheckBoxCell();
cbox = dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[dataGridView.Columns["Options"].Index];
if (cbox.Value  != null)
    {
        // Do some stuff
        MessageBox.Show(cbox.Value.ToString());
    }
 

Whenever I check or uncheck the box, the value is always
null
and so the code to show the MessageBox never runs.
 
I know I'm missing something basic, but I can't see it. Why am I reading
null
all the time, regardless of the checkbox state?
Posted 22-Mar-13 3:10am
Edited 22-Mar-13 3:12am
v2
Comments
josh-jw at 22-Mar-13 8:26am
   
In which datagrid event you are calling this code block?
Fortan_77 at 22-Mar-13 8:28am
   
I'm using _CellContentClick
josh-jw at 22-Mar-13 8:32am
   
check
if (e.RowIndex == -1)
then return ,otherwise do the functionality
josh-jw at 22-Mar-13 8:34am
   
otherwise go for dataGridView1_CellClick event
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
   DataGridViewCheckBoxCell checkbox = (DataGridViewCheckBoxCell)dataGrid_ModelControls.CurrentCell;
 
     bool isChecked = (bool)checkbox.EditedFormattedValue;
 
}
 

Try this ....
  Permalink  
Comments
Fortan_77 at 22-Mar-13 8:54am
   
Thanks Rohit; that works. For any cell I can now see the value:
 
DataGridViewCheckBoxCell checkbox = (DataGridViewCheckBoxCell)dataGridViewBookingDetails.CurrentCell;
bool isChecked = (bool)checkbox.EditedFormattedValue;
MessageBox.Show(isChecked.ToString ());
 
And OriginalGriff, yes it was executing the code... I tried the following:
 
if (dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[dataGridView.Columns["Options"].Index].Value == null)
dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[dataGridView.Columns["Options"].Index].Value = false;
 
MessageBox.Show(dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[dataGridView.Columns["Options"].Index].Value.ToString());
 

So yes Rohit's solution works, but I still don't know what was wrong with my original code.
Fortan_77 at 22-Mar-13 9:04am
   
Ah. Seems that the key is to use "EditedFormattedValue" and NOT "Value".
 
The following works perfectly:
 
if (dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[dataGridView.Columns["colOptionsCot"].Index].EditedFormattedValue != null)
MessageBox.Show(dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[dataGridView.Columns["Options"].Index].EditedFormattedValue.ToString());
 

On the other hand, this falls over with the NullReferenceException that OriginalGriff mentioned above:
 
if (dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[dataGridView.Columns["colOptionsCot"].Index].Value != null)
MessageBox.Show(dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[dataGridView.Columns["Options"].Index].Value.ToString());
Rohit Kumar Mumbai at 22-Mar-13 9:10am
   
Ya Fortran_77 you are right. hope it solves your query .
 
Cheers Mate.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I just tried your code:
private void button1_Click(object sender, EventArgs e)
    {
    dgvData.Rows.Add(true);
    dgvData.Rows.Add(false);
    dgvData.Rows.Add(true);
    dgvData.Rows.Add(false);
    }
 

private void button2_Click(object sender, EventArgs e)
    {
    if (dgvData.Rows[dgvData.CurrentCell.RowIndex].Cells[dgvData.Columns["chkBox"].Index].Value != null)
        {
        // Do some stuff
        MessageBox.Show(dgvData.Rows[dgvData.CurrentCell.RowIndex].Cells[dgvData.Columns["chkBox"].Index].Value.ToString());
        }
    }
And all I get is "true" or "false", as I would expect.
 
Are you sure it is executing that code? Because you would not normally get a MessageBox - you would get a NullReferenceException as the value you are calling ToString on would have to be null - which won't work!
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 210
3 Afzaal Ahmad Zeeshan 204
4 CPallini 185
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 22 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100