i am trying to check the datagridview cells for empty and null value... but i can not do it right...
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if ((String)dataGridView1.Rows[i].Cells[3].Value == String.Empty)
{
MessageBox.Show(" cell is empty");
return;
}
if ((String)dataGridView1.Rows[i].Cells[3].Value == "")
{
MessageBox.Show("cell in empty");
return ;
}
}
i even tried this codes
if (String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value))
{
MessageBox.Show("cell is empty");
return;
}
can any one help me.. with this...
You can find out if it is empty by checking the number of rows in the DataGridView. If myDataGridView. Rows. Count == 0 then your DataGridView is empty.
As for checking whether the DataGridView is empty: If there is no IsEmpty property you may have to look at other things. Maybe looking at RowCount and ColumnCount would help here. Show activity on this post. You can use DataGridView1.
I would try like this:
foreach (DataGridViewRow rw in this.dataGridView1.Rows)
{
for (int i = 0; i < rw.Cells.Count; i++)
{
if (rw.Cells[i].Value == null || rw.Cells[i].Value == DBNull.Value || String.IsNullOrWhiteSpace(rw.Cells[i].Value.ToString())
{
// here is your message box...
}
}
}
if (String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value as String))
{
MessageBox.Show("cell is empty");
return;
}
Add as String
, it works for me.
if (!GridView1.Rows[GridView1.CurrentCell.RowIndex].IsNewRow)
{
foreach (DataGridViewCell cell in GridView1.Rows[GridView1.CurrentCell.RowIndex].Cells)
{
//here you must test for all and then return only if it is false
if (cell.Value == System.DBNull.Value)
{
return false;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With