Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

c# - Error: Deleted row information cannot be accessed through the row

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

ds.Tables[0].Rows[0].Delete();
ds.AcceptChanges();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...