How can I check the null cell value in the DataGridView?
19,971
you can use this
if(!Convert.IsDBNull(dataGridView1.Rows[0].Cells[i].Value))
{
if (i != 0)
{
ar[i] = dataGridView1.Rows[0].Cells[i].Value.ToString ();
}
else
{
ar[i] = dataGridView1.Rows[0].Cells[i].Value.ToString();
}
}
Related videos on Youtube
Author by
priyanka
Updated on June 04, 2022Comments
-
priyanka almost 2 years
How can I check the null cell value in a DataGridView in C#? I've used
DBNull
for checking, but it is not working.Can anybody help me?
The code I currently have is:
string[] ar=new string[dataGridView1.Columns.Count]; for(int i=0;i<dataGridView1.Columns.Count;i++) { if (dataGridView1.Rows[0].Cells[i].Value != DBNull.Value) { if (i != 0) { ar[i] = dataGridView1.Rows[0].Cells[i].Value.ToString (); } else { ar[i] = dataGridView1.Rows[0].Cells[i].Value.ToString(); } }
-
Bala R about 13 yearswhen you say you have tried DBNull, did you try something like
if (dataGridView1.Rows[j].Cells[1].Value == DBNull.Value)
? -
priyanka about 13 yearsthanx for ur help but i've already used this condition but it is not working correctly. But i got my answer of this question
-
Cody Gray about 13 yearsPlease post the code that you've tried so far. Explain to us how it's "not working". What result did you get, compared to what result did you expect?
-
priyanka about 13 yearsi don't know how to attach code ? can u tell me?
-
Cody Gray about 13 yearsI formatted the code for you already. You just indent each line by 4 spaces, or select the block and click the
{}
on the toolbar.
-
-
Rémi over 10 yearsThe correcte function name is
Convert.IsDBNull
I will edit to fix the typo.