如何在验证每一行后从 UltraGrid 和数据库中删除选定的行?
How to delete selected rows from UltraGrid and database after validation of each row?
执行以下函数以从 UltraGrid 和数据库中删除满足条件的所有选定行:
private void RemoveRows()
{
foreach (var row in dgvReceords.Selected.Rows)
{
var order = row.Cells["Order"].Value;
if (order== null || string.IsNullOrEmpty(order.ToString().Trim()))
{
// Update database via business object and then update grid
OrderBO.DeleteByPrimaryKey(row.Cells["Id"].Text);
if (OrderBO.Save() == SaveResult.Success)
row.Delete();
}
}
}
无法遍历所有行并总是留下一行的问题。知道这里出了什么问题吗?
这是因为 foreach()
使用了 .Delete()
正在修改的集合。尝试使用 for()
并以相反的顺序删除:
private void RemoveRows()
{
for (var i=dgvReceords.Selected.Rows.Count-1; i >= 0; i--)
{
var row = dgvReceords.Selected.Rows[i];
var order = row.Cells["Order"].Value;
if (order== null || string.IsNullOrEmpty(order.ToString().Trim()))
{
// Update database via business object and then update grid
OrderBO.DeleteByPrimaryKey(row.Cells["Id"].Text);
if (OrderBO.Save() == SaveResult.Success)
dgvReceords.Selected.Rows[i].Delete();
}
}
}
执行以下函数以从 UltraGrid 和数据库中删除满足条件的所有选定行:
private void RemoveRows()
{
foreach (var row in dgvReceords.Selected.Rows)
{
var order = row.Cells["Order"].Value;
if (order== null || string.IsNullOrEmpty(order.ToString().Trim()))
{
// Update database via business object and then update grid
OrderBO.DeleteByPrimaryKey(row.Cells["Id"].Text);
if (OrderBO.Save() == SaveResult.Success)
row.Delete();
}
}
}
无法遍历所有行并总是留下一行的问题。知道这里出了什么问题吗?
这是因为 foreach()
使用了 .Delete()
正在修改的集合。尝试使用 for()
并以相反的顺序删除:
private void RemoveRows()
{
for (var i=dgvReceords.Selected.Rows.Count-1; i >= 0; i--)
{
var row = dgvReceords.Selected.Rows[i];
var order = row.Cells["Order"].Value;
if (order== null || string.IsNullOrEmpty(order.ToString().Trim()))
{
// Update database via business object and then update grid
OrderBO.DeleteByPrimaryKey(row.Cells["Id"].Text);
if (OrderBO.Save() == SaveResult.Success)
dgvReceords.Selected.Rows[i].Delete();
}
}
}