I have a datagridview with a checkbox column.i want to check whether checkbox is checked or not.
|
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..................
DRISHTYPosted May 20, 2010, 5:33 AM
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");
}
}
Chinthaka BandaraPosted May 20, 2010, 8:11 AM
IT WORKED!!!!
Chinthaka BandaraPosted May 20, 2010, 4:35 AM
but your code didn't solve my error.it is giving me the same error :(
DRISHTYPosted May 20, 2010, 4:24 AM
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
{
}
}
}