1

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?

ravenx30
  • 406
  • 1
  • 6
  • 11

1 Answers1

0

your replecationg the methode. .AcceptChanges use

foreach(DataRow oRow in  mIOptions.mProductsOnList.Where(x.Rowstate = DataRowState.Deleted))
{
 var rowid = oRow["ColumName", DataRowVersion.Original];
//delete row from database
}
mIOptions.mProductsOnList.AcceptChanges()

to accept all deletions/updates/adds

Ivan Sander de Jong
  • 835
  • 1
  • 11
  • 28
  • I have that line after the check as doing it before hand will reset all the states. Its the delete state which is causing issues as I simply need to know the ID of that record so I can delete from database.. – ravenx30 Feb 16 '15 at 12:53
  • updated the code with a linq expresion witch gives you the rows witch will be deleted but normaly you atatch the database to a dataset. when u update this dataset the database gets updated 2. – Ivan Sander de Jong Feb 16 '15 at 13:03
  • I would bind normally, but the dgv takes values from multiple database tables so binding to one is not possible here. Ill try your code in a bit but I thinking your doing the same thing and: var rowid = oRow["IdColumnName"]; will give you the same error.. – ravenx30 Feb 16 '15 at 13:14
  • what exception do you get while retrieving the id using when row["columnName"] in your initial example? – Ivan Sander de Jong Feb 16 '15 at 13:18
  • System.Data.DeletedRowInaccessibleException "Deleted row information cannot be accessed through the row." – ravenx30 Feb 16 '15 at 13:21
  • 1
    Think one solution is just to create an integer array of deleted ID numbers before I do the row.Delete(); then run through the array and delete them all that way. But I do not see the point in DataRowState.Deleted? If i wanted to remove the row so the data is inaccessible I would have used row.remove.. – ravenx30 Feb 16 '15 at 13:26
  • 1
    could you try getting the id with this param row["CustomerID", DataRowVersion.Original] – Ivan Sander de Jong Feb 16 '15 at 13:38
  • Excellent, mPresenterOptions.DeleteProduct(Convert.ToInt32(row[0, DataRowVersion.Original])); Does the job and your answer too will now work thanks to this DataRowVersion attribute :D – ravenx30 Feb 16 '15 at 13:49