简体   繁体   English

c#datagrid问题

[英]c# Problems with datagrid

I have an event that triggers upon a treeview node click, this then creates a datagridview and adds it to a panel: 我有一个事件,该事件在单击树视图节点时触发,然后创建一个datagridview并将其添加到面板中:

void tvd_NodeClickEvent(double animal, string experiment, string pluginIdentifier)
    {

        DataGridDisplay dgv = new DataGridDisplay(panel4);
        dgv.addDatagrid(animal, experiment, pluginIdentifier);

    }

I try to remove the old datagrid by doing the following: 我尝试通过执行以下操作来删除旧的数据网格:

panel4.Controls.Remove(datagrid);
        panel4.Invalidate();

And then adding the new datagrid: 然后添加新的数据网格:

 panel4.Controls.Add(datagrid);

The problem is, the datagrids are drawing themselves on top of eachother without removing the old one? 问题是,数据网格在不删除旧网格的情况下相互吸引? Any ideas? 有任何想法吗?

Thanks. 谢谢。

EDIT: Extra Code added 编辑:添加了额外的代码

void ConfigureDatagrid()
    {
        datagrid.Resize += new EventHandler(datagrid_Resize);

        panel4.Controls.Remove(datagrid);
        panel4.Invalidate();
        datagrid.Location = new System.Drawing.Point(0, 40);
        panel4.Controls.Add(datagrid);

        columnsWidth = datagrid.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);

        checkDatagridControls();

        datagrid.ScrollBars = ScrollBars.Both;
        datagrid.Anchor = (AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right);
        datagrid.Dock = (DockStyle.Top);

        AdjustDatagridHeight();
        AdjustDatagridWidth();

        datagrid.RowHeadersVisible = false;

        datagrid.AutoResizeColumnHeadersHeight();

    }

The code above is called once a new datagrid has been obtained as such: 一旦获得了这样的新数据网格,就会调用上面的代码:

datagrid = file.returnDatagrid(mouse, experiment);
        ConfigureDatagrid();

The panel is passed through from the Winform into the datagrid class. 该面板从Winform传递到datagrid类。

Your code is not complete. 您的代码不完整。 What do "datagrid" refer to exactly and how is it initiated/disposed ? “ datagrid”到底指的是什么,它是如何启动/处置的? in your event ? 在你的事件? Then why don't you handle removal/adding there ? 那你为什么不在那里处理删除/添加呢?

panel4.Controls.Remove(datagrid);
panel4.Controls.Add(datagrid);

What you are basically doing here is just removing the element and then adding it back again. 基本上,您在这里所做的只是删除元素,然后再次将其重新添加。 You should call the first line before your event, and you're done I think. 您应该在活动开始前打电话给第一行,我认为您已经完成了。

Besides, I think you are not doing things properly : Why Remove/Add your DGV anyway and replace it by the same components. 此外,我认为您的操作不正确:为什么仍然要删除/添加DGV,然后用相同的组件替换。 It should NOT be this way. 不应该这样。 since it appears that you are handling the same type of data, then you should affect the DataTable only whithout touching the DataGridView 由于似乎您正在处理相同类型的数据,因此仅应在不触摸DataGridView的情况下影响DataTable

EDIT : 编辑:

According to your edit and comments, this should make more sense : 根据您的编辑和评论,这应该更有意义:

panel4.Controls.Remove(datagrid);
datagrid=file.returnDatagrid();
panel4.Controls.Add(datagrid);
panel4.Invalidate();

You're removing and readding the same datagrid : 您正在删除和读取相同的datagrid

    panel4.Controls.Remove(datagrid);
    panel4.Invalidate();
    datagrid.Location = new System.Drawing.Point(0, 40);
    panel4.Controls.Add(datagrid);

nowhere in this code do you update datagrid so you are trying to remove something that doesn't exist in Controls and then adding it. 这段代码中没有地方更新datagrid因此您尝试删除Controls中不存在的内容,然后添加它。

You need to get your old datagrid to pass to Remove or remove it before update the variable: 您需要让旧的 datagrid传递给Remove或者在更新变量之前将其Remove

    panel4.Controls.Remove(datagrid);
    datagrid=file.returnDatagrid();
    datagrid.Location = new System.Drawing.Point(0, 40);
    panel4.Controls.Add(datagrid);
    panel4.Invalidate();

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

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