简体   繁体   English

无法通过行访问已删除的行信息

[英]Deleted row information cannot be accessed through the row

I have 2 datagridviews containing one table, to give some more space for inserting text. 我有2个datagridviews包含一个表,以提供更多的空间来插入文本。 To add, I don't want to show the first 2 columns, they contain Primary key and Foreign key, I have found a way to add the foreign key just before saving (primary is auto-increment). 要添加,我不想显示前两列,它们包含主键和外键,我找到了一种在保存之前添加外键的方法(主键是自动递增的)。 Now when saving this, all works fine. 现在保存时,一切正常。 But when I want to delete a row.... 但是当我想删除一行时...

Just before the dataset is send back to the database, I add those foreign keys like this, now I have to do it, else the adapt.Update() will throw an error saying that the column can't be null. 就在将数据集发送回数据库之前,我要像这样添加那些外键,现在我必须这样做,否则adapt.Update()将抛出错误,指出该列不能为null。

    private void SaveAnalyseTable()
    {
        dgvAnalyse1.EndEdit();
        dgvAnalyse2.EndEdit();
        for (int x = 0; x < dgvAnalyse1.Rows.Count-1; x++) //add to table, -1 else it will try to insert Id into an empty row (out of index error)
        {
            if (x != dgvAnalyse1.Rows.Count) //if it is the last one, don't execute
            {
                if (string.IsNullOrEmpty(patientsDataset.Tables["Analyse"].Rows[x]["PatientId"].ToString()) && (patientsDataset.Tables["Analyse"].Rows[x].RowState != DataRowState.Deleted)) .
                {
                    patientsDataset.Tables["Analyse"].Rows[x]["PatientId"] = currentPatient.PatientId;
                }
            }

        }
    }

In the above code, I can't add those foreign numbers to the datagridview since the column isn't there The error is thrown in the code above here, when I'm trying to delete a row like this: 在上面的代码中,由于列不存在,所以无法将这些外来数字添加到datagridview中。当我尝试删除这样的行时,在上面的代码中引发了错误:

    private void btnAnalyseDelete_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow item in dgvAnalyse1.SelectedRows)
        {
            dgvAnalyse1.Rows.RemoveAt(item.Index);
            int index = item.Index;
            if (index != -1)
            {
                patientsDataset.Tables["Analyse"].Rows[index].Delete();
            }

        }
        SaveAllData(); //even when saving the whole dataset to the database, still get the error

    }

So: first saving, then deleting doesn't work, it throws the error right away. 因此:先保存然后删除不起作用,它会立即引发错误。 deleting before saving it does work, it just can't be the same index, so it has to do something with the row in the dataset I guess. 在保存之前删除它确实起作用,只是不能使用相同的索引,因此它必须对我猜数据集中的行进行某些处理。

Editing -> updateDataSet -> SaveToDatabase -> Delete row -> error. 编辑-> updateDataSet-> SaveToDatabase->删除行->错误。

Editing -> UpdateDataSet -> deleteRow ->updateDataSet -> SaveToDatabase -> fine unless same index 编辑-> UpdateDataSet-> deleteRow-> updateDataSet-> SaveToDatabase->很好,除非相同的索引

I'm sure that when saving the data, it will re-fill the dataset of tables. 我确定在保存数据时,它将重新填充表的数据集。

When you call SaveAnalyseTable you have a test to avoid DataRowState.Deleted rows and rows with null or empty values for PatientID column but your logic should consider the effect of short circuit evaluation of the && operator 当您调用SaveAnalyseTable您有一个避免使用DataRowState.Deleted的测试。 DataRowState.Deleted行以及PatientID列的值为空或空的行,但是您的逻辑应考虑对&&运算符进行短路评估的影响

In your case, the test for the PatientID being null or empty happens before the test for DataRowState.Deleted status and thus it is executed also in case of deleted rows. 在您的情况下,对PatientID为null或为空的测试会在对DataRowState.Deleted状态的测试之前进行,因此在删除行的情况下也会执行该测试。 The operator && first tries to verify the first part of the expression ( PatientID is null or empty) but the check for deleted happens only after the first one. 运算符&&首先尝试验证表达式的第一部分( PatientID为null或为空),但是仅在第一部分之后才进行删除检查。 So the first test happens every time and this test could potentially try to access a DataRowState.Deleted row. 因此,第一次测试每次都会发生,并且该测试可能会尝试访问DataRowState.Deleted行。

You need only to reverse the order in which you test the row for null or empty 您只需要颠倒测试行为null或空的顺序

if (patientsDataset.Tables["Analyse"].Rows[x].RowState != DataRowState.Deleted && 
    string.IsNullOrEmpty(patientsDataset.Tables["Analyse"].Rows[x]["PatientId"].ToString())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM