致相关人员,经过长时间搜索,我仍无法解决以下错误:
"已删除的行信息无法通过行访问"
我知道从datatable中删除一行后,它就无法像通常那样被访问了,这就是为什么我会收到这个错误消息。问题在于,我不确定该怎么做才能得到我想要的结果,下面我将进行说明。
基本上,当“dg1”中的一行被删除时,其下方的行将取代删除的行(显然),并因此继承了被删除行的索引。本方法的目的是通过从相应数据集的对应值中获取索引值来替换和重置取代删除行的行的索引。
现在,我只使用一个标签(lb1Text)尝试从该处理过程中获得响应,但是最后一个嵌套的if语句尝试比较值时程序就会崩溃。
以下是代码:
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'
}
}
}
非常感谢您提前的帮助,我确实进行过搜索,如果通过简单的谷歌搜索很容易弄清楚,那么请允许我一遍又一遍地自责,因为我真的尝试过了。
- gc