Click here to Skip to main content
12,395,358 members (63,422 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 2:10am
Updated 22-Mar-13 2:12am
v2
Comments
josh-jw 22-Mar-13 8:26am
   
In which datagrid event you are calling this code block?
Fortan_77 22-Mar-13 8:28am
   
I'm using _CellContentClick
josh-jw 22-Mar-13 8:32am
   
check
if (e.RowIndex == -1)
then return ,otherwise do the functionality
josh-jw 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 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 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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 22 Mar 2013
Copyright © CodeProject, 1999-2016
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