[英]How to check empty and null cells in datagridview using C#
i am trying to check the datagridview cells for empty and null value... but i can not do it right...我正在尝试检查 datagridview 单元格中的空值和空值...但我做不好...
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...谁能帮我这个...
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...
}
}
}
I think you should use this:我认为你应该使用这个:
for (int i = 0; i < dataGridView1.RowCount; i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value == DBNull.Value)
{
dataGridView1.Rows[i].Cells[j].Value = "null";
}
}
}
dataGridView1.Update();
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 (String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value as String))
{
MessageBox.Show("cell is empty");
return;
}
Add as String
, it works for me.添加
as String
,它对我有用。
我认为你应该先检查 null
Convert.IsDBNull(dataGridView1.Rows[j].Cells[1].FormattedValue)
Try this :尝试这个 :
foreach (DataGridViewRow row in dataGridView.Rows)
{
IEnumerable<DataGridViewCell> cellsWithValusInRows = from DataGridViewCell cell in row.Cells
where string.IsNullOrEmpty((string)cell.Value)
select cell;
if (cellsWithValusInRows != null && cellsWithValusInRows.Any())
{
//Then cells with null or empty values where found
}
}
Then check for the collection if it is null or it has elements.然后检查集合是否为空或是否有元素。
I hope this was helpful.我希望这可以帮到你。
for (int i = 0; i < GV1.Rows.Count; i++)
{
if ((String)GV1.Rows[i].Cells[4].Value == null)
{
MessageBox.Show(" cell is empty");
return;
}
}
It's working fine.它工作正常。
Works perfect for me:非常适合我:
for (int i = dataGridView1.Rows.Count - 1; i >= 0; i--)
{
DataGridViewRow dataGridViewRow = dataGridView1.Rows[i];
foreach (DataGridViewCell cell in dataGridViewRow.Cells)
{
string val = cell.Value as string;
if (string.IsNullOrEmpty(val))
{
if (string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value as string)) // If you want to check more then just one cell you could also add "&& (string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[ANY NUMBER].Value as string)
{
MessageBox.Show(" cell is empty");
return;
/* or to delete replace with:
dataGridView1.Rows.Remove(dataGridViewRow);
break;
*/
}
}
}
}
// move cell value to Excel
for (int i = 0; i < grd.Rows.Count; i++)
{
for (int t = 0; t < grd.Columns.Count; t++)
{
try
{
worksheet.Cells[i + 2, t + 1] = grd.Rows[i].Cells[t].Value.ToString();
}
catch (Exception e)
{
Debug.WriteLine(e.HResult + " " + e.Message + " I've always hated null ");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.