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

标签 c# datagrid dataset runtime

这可能与谁有关,我已经搜索了相当多的时间,以找到解决此错误的方法

"Deleted row information cannot be accessed through the row"

我知道一旦从数据表中删除了一行,就无法以典型方式访问它,这就是我收到此错误的原因。最大的问题是我不确定该怎么做才能获得我想要的结果,我将在下面概述。

基本上,当“dg1”中的一行被删除时,它下面的行将取代已删除的行(很明显),因此继承了已删除的行索引。此方法的目的是替换和重置代替已删除行的行索引(通过从数据集中的相应值中获取它)以及索引值。

现在我只是使用一个标签 (lblText) 来尝试从进程中获取响应,但是当最后一个嵌套的 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

最佳答案

您还可以使用 DataSet AcceptChanges() 方法来应用已删除的完整内容。

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

关于c# - 错误 : Deleted row information cannot be accessed through the row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4321840/

相关文章:

javascript - 在 PostBackUrl 中使用 JavaScript 变量

c# - 计算程序生成的圆形网格的 uv 纹理坐标

c# - 在没有 silverlight 项目的情况下使用 RIA 服务

javascript - jqGrid 子网格的 JSON 对象

c# - WPF 绑定(bind) DataGrid - 我可以添加一个未绑定(bind)的列来显示状态吗?

opencv - 如何找到 facemark-> OpenCV_Contrib 加载模型的模型文件?

c# - 我应该使方法虚拟还是抽象?

c# - 创建仅在运行时已知类型的变量 (C#)

java - JFreeChart 未更新

tensorflow - 如何创建带有用于图像分割的掩码的自定义图像数据集?(特别是针对 Tensorflow)