简体   繁体   English

如何通过从树节点中选择一个节点来从数据网格中删除一行?

[英]How to delete a row from datagrid by selecting a node from treenode?

Iam entering data in datagridview in c# when i draw a rectangle on picturebox, i have an array where i store the rectangle data, whenever i enter a new row, i also create a new childnode in treeview. Now i want to delete a row in datagridview by selecting the corresponding treenode.当我在 picturebox 上绘制一个矩形时,我在 c# 的 datagridview 中输入数据,我有一个存储矩形数据的数组,每当我输入一个新行时,我也在 treeview 中创建一个新的子节点。现在我想删除一行datagridview 通过选择相应的树节点。 But my code is not working properly.但是我的代码无法正常工作。 I mostly get out of range exception errors.我主要遇到超出范围的异常错误。

Please help me fix this?请帮我解决这个问题?

This is my code to delete the row:这是我删除行的代码:

         int pos = (int) treeView1.SelectedNode.Tag;
         
            if (treeView1.SelectedNode.Name.Contains("Code"))
            {
                if (pos == 0)
                {
                    MessageBox.Show("Cannot delete 1st node");
                }

                else
                {
                    treeView1.SelectedNode.Remove();

                    for (int i = pos; i < rectincr; i++)
                    {
                        PB1CodeRect[i] = PB1CodeRect[i + 1]; //delete rectangle with row and node
                        dataGridView2.Rows[i].Tag = pos;
                        dataGridView2.Rows[poss].Selected = true;

                        foreach (DataGridViewRow item in this.dataGridView2.SelectedRows)
                        {
                            dataGridView2.Rows.RemoveAt(item.Index); //delete dgvrow
                        }
                    }
                  
                   
                }

And this is the part of code where dgv and node is inserted.这是插入 dgv 和节点的代码部分。

       {
       PB1CodeRect[pb1Coderectincr] = new Rectangle(Convert.ToInt32(cibcam1.SelectionRegion.X), Convert.ToInt32(cibcam1.SelectionRegion.Y), Convert.ToInt32(cibcam1.SelectionRegion.Width), Convert.ToInt32(cibcam1.SelectionRegion.Height));
            pb1Coderectincr++;
            this.dataGridView2.Rows.Add(pb1Coderectincr, tabControl2.SelectedTab.Text, "2dCode - " + cibcam1.SelectionRegion.X + "," + cibcam1.SelectionRegion.Y + "," + cibcam1.SelectionRegion.Width + "," + cibcam1.SelectionRegion.Height,0);

            codenodcam1.Name = "Code ROI-" + pb1Coderectincr;
            codenodcam1.Text = "Code ROI-" + pb1Coderectincr;
            codenodcam1.Tag = pb1Coderectincr + 1;
           //var dict = (Dictionary<int, int>)codenodcam1.Nodes[0].Tag;
           // dict[pb1Coderectincr] = pb1Coderectincr;
            mainNode.Nodes.Add(codenodcam1);
            mainNode.ExpandAll();
           }

I found the solution.我找到了解决方案。

In reply to JohnG's questions - im building an image processing application where I draw an ROI, the roi rectangle coordinates are saved in the PBRect[] array because there would be multiple rectangles.在回答 JohnG 的问题时——我构建了一个图像处理应用程序,我在其中绘制了一个 ROI,roi 矩形坐标保存在 PBRect[] 数组中,因为会有多个矩形。 Each rectangle's coordinates, index, inspection result are stored in a row of DGV.每个矩形的坐标、索引、检测结果存储在一行DGV中。 The treeview is to allow the user easy access to manipulate ROI size, delete roi etc. The DGV is hidden from user view. treeview 允许用户轻松访问操作 ROI 大小、删除 roi 等。DGV 从用户视图中隐藏。

I made the mistake of adding pb1Coderectincr++ before i updated values, so the increment has to be the last one.我在更新值之前错误地添加了 pb1Coderectincr++,因此增量必须是最后一个。

I also updated the DGV index value once a node, rect and row has been deleted so the index is not messed up.一旦节点、矩形和行被删除,我也更新了 DGV 索引值,这样索引就不会混乱。 Here is the working code to delete node, row and rectangle:下面是删除节点、行和矩形的工作代码:

       int pos = (int)treeView1.SelectedNode.Index;
       if (pos == 0)
                {
                    MessageBox.Show("Cannot delete 1st node");
                }
                else
                {
                    treeView1.SelectedNode.Remove(); //delete node
                    for (int i = pos; i < pb1Coderectincr; i++) //delete rectangle
                    {
                        PB1CodeRect[i] = PB1CodeRect[i + 1];
                    }
                    dataGridView2.Rows[pos].Selected = true;
                    foreach (DataGridViewRow item in this.dataGridView2.SelectedRows) //delete dgv row
                    {
                        dataGridView2.Rows.RemoveAt(item.Index);
                        if (!item.IsNewRow) // update index
                        {
                            item.Cells[0].Value = item.Index + 1;
                        }
                    }
                    treeView1.Focus();
                }

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

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