Click here to Skip to main content
15,891,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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

C#
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
C#
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
C#
null
all the time, regardless of the checkbox state?
Posted
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

C#
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
   DataGridViewCheckBoxCell checkbox = (DataGridViewCheckBoxCell)dataGrid_ModelControls.CurrentCell;

     bool isChecked = (bool)checkbox.EditedFormattedValue;

}



Try this ....
 
Share this answer
 
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.
I just tried your code:
C#
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!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900