【问题标题】:This row belongs to another table error in C# [duplicate]此行属于 C# 中的另一个表错误 [重复]
【发布时间】:2014-06-05 08:09:39
【问题描述】:

我有 2 个 DataGridView,即 dgvEmpEnrolleesdgvEmpMustAttend,它们有一个 DataTable 作为数据源,分别是 dtEnrolleesdtMustAttend

我有一个btnRemove,它删除了dgvEmpEnrollees datagridview dtEnrollees DataTable 中当前选定的行,并立即更新了 Datagridview,我希望将同一行添加到第二个 DT 和 DGV 中。

注意:这 2 个数据表是克隆的:

btnRemove代码sn-p如下:

    private void btnRemove_Click(object sender, EventArgs e)
        {
            DataRowView currentDataRowView = (DataRowView)dgvEmpEnrollees.CurrentRow.DataBoundItem;
            DataRow row = currentDataRowView.Row;

            dtEnrollees.Rows.Remove(row);
            dgvEmpEnrollees.DataSource = null;
            dgvEmpEnrollees.DataSource = dtEnrollees;


            dtMustAttend.Rows.InsertAt(row,0);
            dgvEmpMustAttend.DataSource = null;
            dgvEmpMustAttend.Rows.Clear();
            dgvEmpMustAttend.DataSource = dtMustAttend;

        }

此行属于另一个表C# 之后提示错误。

我也试过dtMustAttend.ImportRow(row);的方法,它不提示错误但不会添加行。 任何帮助将不胜感激。

【问题讨论】:

  • 你试过克隆行吗?

标签: c# winforms datagridview datatable


【解决方案1】:

该行确实从DataTable 中删除了这一行:

dtEnrollees.Rows.Remove(row);

还会从该行中删除所有数据。如果你在Remove()之后检查ItemArray-property,它会声明:

'test.ItemArray' threw an exception of type 'System.Data.RowNotInTableException'

另一个问题是该行仍然将Table-property 设置为它所在的表。一开始你使用InsertAt()-方法,但它失败了,因为Table-property 仍然设置为其他表。

当您使用ImportRow() 时,该行已清除为ItemArray,因此您试图添加一个空行。

【讨论】:

    【解决方案2】:

    我找到了解决方案,但我不知道它背后的解释。

    我尝试交换导入和删除代码(首先导入)并且成功了。

    工作代码sn-p:

     private void btnRemove_Click(object sender, EventArgs e)
            {
                DataRowView currentDataRowView = (DataRowView)dgvEmpEnrollees.CurrentRow.DataBoundItem;
                DataRow row = currentDataRowView.Row;
    
                dtMustAttend.ImportRow(row);
                dgvEmpMustAttend.DataSource = null;
                dgvEmpMustAttend.Rows.Clear();
                dgvEmpMustAttend.DataSource = dtMustAttend;
    
                dtEnrollees.Rows.Remove(row);
                dgvEmpEnrollees.DataSource = null;
                dgvEmpEnrollees.DataSource = dtEnrollees;
    
            }
    

    我不知道如果删除事务先出现,代码为什么不起作用:

     private void btnRemove_Click(object sender, EventArgs e)
            {
                DataRowView currentDataRowView = (DataRowView)dgvEmpEnrollees.CurrentRow.DataBoundItem;
                DataRow row = currentDataRowView.Row;
    
                dtEnrollees.Rows.Remove(row);
                dgvEmpEnrollees.DataSource = null;
                dgvEmpEnrollees.DataSource = dtEnrollees;
    
                dtMustAttend.ImportRow(row);
                dgvEmpMustAttend.DataSource = null;
                dgvEmpMustAttend.Rows.Clear();
                dgvEmpMustAttend.DataSource = dtMustAttend;
    
            }
    

    任何解释将不胜感激。

    【讨论】:

    • 请将此作为附录添加到您的帖子中,而不是作为更多问题的答案。
    猜你喜欢
    • 2010-10-17
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    相关资源
    最近更新 更多