RowNotInTableException when accessing second time

2019-08-16 07:25发布

I faced a problem when trying to access row again.

I have TreeView component and in Tag property I put row from datatable:

foreach (DataRow row in catalogDataSet.category)
        {
            if (row["parent"].ToString().Equals("0"))
            {
                TreeNode node = new TreeNode();
                node.Text = row["name"].ToString();
                node.Tag = row;
                string nodeId = row["id"].ToString();
                if (this.HasChilds(nodeId))
                {
                    this.fillChilds(nodeId, node);
                }
                tree.Nodes.Add(node);
            }

        }

Then I try to access row`s id:

int locationId = int.Parse(((DataRow)locationTreeView.SelectedNode.Tag)["id"].ToString());

First time it's ok, but after event click fires and loads product`s info into panel second time I try to load dataGridView by id from row (same code above) I get error:

RowNotInTableException:

This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.

public Panel GetPreviewPanel(int productId)
    {
        InitializeComponent();
        tableLayoutPanel4.Controls.Add(this.closeButton, 2, 0);
        categoryApi.FillComboBox(this.categoryComboBox);
        warehouseApi.FillWarehouseCombobox(this.warehouseComboBox);
        warehouseComboBox.SelectionChangeCommitted += new EventHandler(LocationComboBoxSelectionChangeCommitted);
        label.Text = "Peržiūrėti prekę";
        //fill locations with loaded warehouse value
        locationApi.FillLocationComboboxByWarehouse(this.locationComboBox, (int)((ComboItem)this.warehouseComboBox.SelectedItem).Value);
        categoryComboBox.SelectionChangeCommitted += new EventHandler(CategoryComboBoxSelectionChangeCommitted);
        //setting default values
        CatalogDataSet.productRow product = productApi.GetProductData(productId);
        if (product == null)
            return null;
        productNameTextBox.Text = product["name"].ToString();
        productNameTextBox.Enabled = false;
        return this.productPanel;
    }

0条回答
登录 后发表回答