38

To whom this may concern, I have searched a considerable amount of time, to work a way out of this error

"Deleted row information cannot be accessed through the row"

I understand that once a row has been deleted from a datatable that it cannot be accessed in a typical fashion and this is why I am getting this error. The big issue is that I am not sure what to do to get my desired result, which I will outline below.

Basically when a row in "dg1" is deleted the row beneath it takes the place of the deleted row (obviously) and thus inherits the deleted rows index. The purpose of this method is to replace and reset the rows index (via grabbing it from the corresponding value in the dataset) that took the deleted rows place and as such the index value.

Right now I am just using a label (lblText) to try and get a response from the process, but it crashes when the last nested if statement trys to compare values.

Here is the code:

void dg1_Click(object sender, EventArgs e)
    {
        rowIndex = dg1.CurrentRow.Index; //gets the current rows
        string value = Convert.ToString(dg1.Rows[rowIndex].Cells[0].Value);

        if (ds.Tables[0].Rows[rowIndex].RowState.ToString() == "Deleted")
        {

            for (int i = 0; i < dg1.Rows.Count; i++)
            {

                if (Convert.ToString(ds.Tables[0].Rows[i][0].ToString()) == value) 
                // ^ **where the error is occurring**
                {
                    lblTest.Text = "Aha!";
                    //when working, will place index of compared dataset value into                                   rowState, which is displaying the current index of the row I am focussed on in 'dg1'
                }
            }
        }

Thanks ahead of time for the help, I really did search, and if it is easy to figure out through a simple google search then allow myself to repeatably hate on me, because I DID try.

  • gc

4 Answers 4

56

You can also use the DataSet's AcceptChanges() method to apply the deletes fully.

ds.Tables[0].Rows[0].Delete();
ds.AcceptChanges();
1
  • 1
    still relevant in 2021
    – MX313
    Nov 5, 2021 at 19:45
39

The current value for the data column in the inner if statement will not be available for deleted rows. To retrieve a value for deleted rows, specify that you want the original value. This should fix your error:

if (Convert.ToString(ds.Tables[0].Rows[i][0, DataRowVersion.Original].ToString()) == value)
3
  • Why do you use Convert.ToString when you've already used .ToString()? This seems redundant. Sep 27, 2015 at 0:42
  • @druciferre - I agree it is redundant. I copied the OP's original code and modified it to use the original value of the DataRow.
    – firedfly
    Sep 28, 2015 at 18:13
  • i think ds.Tables[0].Acceptchanges() will lineup with row index of datagridview Dec 14, 2018 at 22:56
16

In your "crashing if", you can check if the row is deleted before accessing it's values :

if (ds.Tables[0].Rows[i].RowState != DataRowState.Deleted &&
    Convert.ToString(ds.Tables[0].Rows[i][0].ToString()) == value)
{
    // blaaaaa
}

Also, I'm not sure why you ToString() the RowState instead of comparing it to DataRowState.Deleted.

0

after deleting the row , rebind your grid with the datatable , no need to manually resetting index , datatable handels it.

so you onl;y need to rebind grid's datasource.

2
  • I am sorry I should have clarified, I am not attempting to update the datasource at all, I am trying to make it so that, no matter the location of the rows in the datagrid, the datagrid will always show the index of that information as it is in the dataset. ie - row 2 of the datagrid returns an index of 1 but in the dataset, the index of that row is 2. without committing the delete to the database, i need to grab the index of that row from the dataset and place it into the datagrid so the corresponding row in dg (row with the same information) shows an index of 2.
    – jshbrmn
    Dec 1, 2010 at 7:23
  • perhaps i am confused by your explanation, i am still getting used to the logic of c# database access.
    – jshbrmn
    Dec 1, 2010 at 7:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.