I have a delete button which removes items from the DataGridVeiw and sets the main DataTable.Row.RowState to Deleted. Problem arises when I want to save all changes but can no longer access the a value (ID) from the row which state has changed. Ill show you the code:
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;
}
}
}
}
Now the Check:
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();
So what is the normal work-around for this?