Search code examples
c#datagridviewstrongly-typed-dataset

Exception after removing Row from DataSet


My Basci setting: DataSet with 3 DataTables, no relations; 2 DataBindings one for the Version-table and on for the Script-table 2 Datagridviews which have the databindings as datasources

On a button-click-event i get the first selected row in the Version-datagridview, get the DataboundItem and cast it as "VersionRow". Then get the row in the dataset and use Delete(); -methode.

This all runs without exceptions at all, and in Debug you see that the state of the versionrow in the dataset is changed as well. But then it's crashing with a DeletedRowInaccessibleException.

ClickEvent with Deleting:

private void VersionDel_Click(object sender, EventArgs e)
    {
        if (VersionDataGird.SelectedRows.Count > 0)
        {
            var x = (MainDB.VersionsRow)VersionDataGird.SelectedRows[0].DataBoundItem;
            mainDB.Versions.FindByID(x.ID).Delete(); 
        }

    }//after here it jumps in a generated function to get id from dataset and fails

I expected that the row only would disappear at the DataGridView when I delete it at DataSet. Do you know where my mistake is?


Solution

  • I think you are getting the Selected Row and deleting it from the database. But you are not updating the Grid View.

    Try to remove the datasource of the Data Grid once you are deleting the row and after deleting the row add the data source to this grid once again.

    You can also use adapter instead of this. here is a link to tutorial I found in the Internet.

    Hope it helps !