我有一个删除按钮,它从DataGridVeiw中删除项目,并将主DataTable.Row.RowState设置为Deleted。当我想保存所有更改,但无法再访问状态已更改的行中的值(ID)时,就会出现问题。我给你看代码:
private void btnRemove_Click(object sender, EventArgs e)
{
if (dgvProducts.CurrentRow != null)
{
foreach(DataRow row in mIOptions.mProductsOnList.Rows)
{
if (dgvProducts.CurrentRow.Cells[2].Value == row[2] && txtProdCode.Text == row[1].ToString())
{
dgvProducts.Rows.Remove(dgvProducts.CurrentRow);
row.Delete();
break;
}
}
}
}
现在检查:
foreach (DataRow row in mIOptions.mProductsOnList.Rows)
{
if (row.RowState == DataRowState.Modified)
{
mPresenterOptions.UpdateProduct(row);
}
else if (row.RowState == DataRowState.Added)
{
mPresenterOptions.SaveProduct(row);
}
else if (row.RowState == DataRowState.Deleted)
{
mPresenterOptions.DeleteProduct(row); // *row no longer contains the ID (needed to delete from db)*
}
}
mIOptions.mProductsOnList.AcceptChanges();
那么,这方面的正常工作是什么呢?
您对方法的补充。.接受更改使用
foreach(DataRow oRow in mIOptions.mProductsOnList.Where(x.Rowstate = DataRowState.Deleted))
{
var rowid = oRow["ColumName", DataRowVersion.Original];
//delete row from database
}
mIOptions.mProductsOnList.AcceptChanges()
接受所有删除/更新/添加