I have a datagridview with a checkbox column.i want to check whether checkbox is checked or not.
C# |
if the checkbox is selected it will output as "True" in messagebox.but if checkbox is not selected it is giving me an error
"Object reference not set to an instance of an object."
i want to get output when checkbox is not checked also
please help me....
Thank you..................

DRISHTY134 reputationPosted May 20, 2010, 1:33 AM EDT
Try this..
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
{
string values = dataGridView1.Rows[i].Cells[1].Value.ToString();
MessageBox.Show("checked");
}
else
{
string values = dataGridView1.Rows[i].Cells[1].Value.ToString();
MessageBox.Show("unchecked");
}
}
DRISHTY134 reputationPosted May 20, 2010, 12:24 AM EDT
try this,
private void btn_Generate_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
if(dataGridView1.Rows[i].Cells[4].Value==True)
{
Messtring values = dataGridView1.Rows[i].Cells[4].Value.ToString();
MessageBox.Show(values);
sageBox.Show(values);
}
else
{
}
}
}
Chinthaka Bandara33 reputationPosted May 20, 2010, 12:35 AM EDT
but your code didn't solve my error.it is giving me the same error :(
Chinthaka Bandara33 reputationPosted May 20, 2010, 4:11 AM EDT
IT WORKED!!!!