C# 更改控件父级将删除内容

C# 更改控件父级将删除内容,c#,winforms,datagridview,parent,tabcontrol,C#,Winforms,Datagridview,Parent,Tabcontrol,在我的应用程序中,我有一个DataGridView,它可以从一个选项卡控件移动到另一个选项卡控件 我通过改变它的父对象来实现这一点。这在第一次从原始区域选项卡控件移动到新控件时不会出现问题,但是当将父控件更改回原始区域时,会显示DataGridView(所有列都可见),但视图中没有数据 我尝试将数据重新加载到DataGridView中,并刷新/使控件无效以使其重新绘制,但它仍然显示为空。但是,当控件返回到次父级时,数据将返回 我还在另一个DataGridView中使用这段代码,它可以毫无问题地工

在我的应用程序中,我有一个DataGridView,它可以从一个选项卡控件移动到另一个选项卡控件

我通过改变它的父对象来实现这一点。这在第一次从原始区域选项卡控件移动到新控件时不会出现问题,但是当将父控件更改回原始区域时,会显示DataGridView(所有列都可见),但视图中没有数据

我尝试将数据重新加载到DataGridView中,并刷新/使控件无效以使其重新绘制,但它仍然显示为空。但是,当控件返回到次父级时,数据将返回

我还在另一个DataGridView中使用这段代码,它可以毫无问题地工作

如果您有任何想法,我们将不胜感激,并提前表示感谢

从原始到次级

gvwRFIs.Parent = tabProcessingRFI; //Working
gvwConsentInfoMemos.Parent = tabProcessingMemos; //Working
从次级到原始

gvwRFIs.Parent = tabConsentInfoRFI; //Empty Data
gvwConsentInfoMemos.Parent = tabConsentInfoMemos; //Working
RFIDataGridView设计器代码

        // 
        // gvwRFIs
        // 
        this.gvwRFIs.AllowUserToAddRows = false;
        this.gvwRFIs.AllowUserToDeleteRows = false;
        this.gvwRFIs.AllowUserToResizeRows = false;
        this.gvwRFIs.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
        this.gvwRFIs.BackgroundColor = System.Drawing.Color.White;
        this.gvwRFIs.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.gvwRFIs.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.gvwID,
        this.gvwType,
        this.gvwSeq,
        this.gvwCreated,
        this.gvwProcessor,
        this.gvwLetter,
        this.gvwResponded,
        this.gvwS,
        this.gvwDetails});
        this.gvwRFIs.Dock = System.Windows.Forms.DockStyle.Fill;
        this.gvwRFIs.Location = new System.Drawing.Point(3, 3);
        this.gvwRFIs.MultiSelect = false;
        this.gvwRFIs.Name = "gvwRFIs";
        this.gvwRFIs.ReadOnly = true;
        this.gvwRFIs.RowHeadersVisible = false;
        this.gvwRFIs.RowHeadersWidth = 4;
        this.gvwRFIs.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        this.gvwRFIs.Size = new System.Drawing.Size(1078, 422);
        this.gvwRFIs.TabIndex = 4;
        this.gvwRFIs.DoubleClick += new System.EventHandler(this.gvwRFIs_DoubleClick);
        // 
        // tabConsentInfoRFI
        // 
        this.tabConsentInfoRFI.Controls.Add(this.gvwRFIs);
        this.tabConsentInfoRFI.Controls.Add(this.lvwConsentInfoRFI);
        this.tabConsentInfoRFI.Location = new System.Drawing.Point(4, 32);
        this.tabConsentInfoRFI.Name = "tabConsentInfoRFI";
        this.tabConsentInfoRFI.Padding = new System.Windows.Forms.Padding(3);
        this.tabConsentInfoRFI.Size = new System.Drawing.Size(1084, 428);
        this.tabConsentInfoRFI.TabIndex = 4;
        this.tabConsentInfoRFI.Text = "RFI\'s";
        this.tabConsentInfoRFI.UseVisualStyleBackColor = true;
        // 
        // tabProcessingRFI
        // 
        this.tabProcessingRFI.Location = new System.Drawing.Point(4, 36);
        this.tabProcessingRFI.Name = "tabProcessingRFI";
        this.tabProcessingRFI.Padding = new System.Windows.Forms.Padding(3);
        this.tabProcessingRFI.Size = new System.Drawing.Size(868, 465);
        this.tabProcessingRFI.TabIndex = 1;
        this.tabProcessingRFI.Text = "RFI";
        this.tabProcessingRFI.UseVisualStyleBackColor = true;
同意选项卡控件设计器代码

        // 
        // gvwRFIs
        // 
        this.gvwRFIs.AllowUserToAddRows = false;
        this.gvwRFIs.AllowUserToDeleteRows = false;
        this.gvwRFIs.AllowUserToResizeRows = false;
        this.gvwRFIs.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
        this.gvwRFIs.BackgroundColor = System.Drawing.Color.White;
        this.gvwRFIs.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.gvwRFIs.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.gvwID,
        this.gvwType,
        this.gvwSeq,
        this.gvwCreated,
        this.gvwProcessor,
        this.gvwLetter,
        this.gvwResponded,
        this.gvwS,
        this.gvwDetails});
        this.gvwRFIs.Dock = System.Windows.Forms.DockStyle.Fill;
        this.gvwRFIs.Location = new System.Drawing.Point(3, 3);
        this.gvwRFIs.MultiSelect = false;
        this.gvwRFIs.Name = "gvwRFIs";
        this.gvwRFIs.ReadOnly = true;
        this.gvwRFIs.RowHeadersVisible = false;
        this.gvwRFIs.RowHeadersWidth = 4;
        this.gvwRFIs.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        this.gvwRFIs.Size = new System.Drawing.Size(1078, 422);
        this.gvwRFIs.TabIndex = 4;
        this.gvwRFIs.DoubleClick += new System.EventHandler(this.gvwRFIs_DoubleClick);
        // 
        // tabConsentInfoRFI
        // 
        this.tabConsentInfoRFI.Controls.Add(this.gvwRFIs);
        this.tabConsentInfoRFI.Controls.Add(this.lvwConsentInfoRFI);
        this.tabConsentInfoRFI.Location = new System.Drawing.Point(4, 32);
        this.tabConsentInfoRFI.Name = "tabConsentInfoRFI";
        this.tabConsentInfoRFI.Padding = new System.Windows.Forms.Padding(3);
        this.tabConsentInfoRFI.Size = new System.Drawing.Size(1084, 428);
        this.tabConsentInfoRFI.TabIndex = 4;
        this.tabConsentInfoRFI.Text = "RFI\'s";
        this.tabConsentInfoRFI.UseVisualStyleBackColor = true;
        // 
        // tabProcessingRFI
        // 
        this.tabProcessingRFI.Location = new System.Drawing.Point(4, 36);
        this.tabProcessingRFI.Name = "tabProcessingRFI";
        this.tabProcessingRFI.Padding = new System.Windows.Forms.Padding(3);
        this.tabProcessingRFI.Size = new System.Drawing.Size(868, 465);
        this.tabProcessingRFI.TabIndex = 1;
        this.tabProcessingRFI.Text = "RFI";
        this.tabProcessingRFI.UseVisualStyleBackColor = true;
处理选项卡控件设计器代码

        // 
        // gvwRFIs
        // 
        this.gvwRFIs.AllowUserToAddRows = false;
        this.gvwRFIs.AllowUserToDeleteRows = false;
        this.gvwRFIs.AllowUserToResizeRows = false;
        this.gvwRFIs.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
        this.gvwRFIs.BackgroundColor = System.Drawing.Color.White;
        this.gvwRFIs.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.gvwRFIs.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.gvwID,
        this.gvwType,
        this.gvwSeq,
        this.gvwCreated,
        this.gvwProcessor,
        this.gvwLetter,
        this.gvwResponded,
        this.gvwS,
        this.gvwDetails});
        this.gvwRFIs.Dock = System.Windows.Forms.DockStyle.Fill;
        this.gvwRFIs.Location = new System.Drawing.Point(3, 3);
        this.gvwRFIs.MultiSelect = false;
        this.gvwRFIs.Name = "gvwRFIs";
        this.gvwRFIs.ReadOnly = true;
        this.gvwRFIs.RowHeadersVisible = false;
        this.gvwRFIs.RowHeadersWidth = 4;
        this.gvwRFIs.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        this.gvwRFIs.Size = new System.Drawing.Size(1078, 422);
        this.gvwRFIs.TabIndex = 4;
        this.gvwRFIs.DoubleClick += new System.EventHandler(this.gvwRFIs_DoubleClick);
        // 
        // tabConsentInfoRFI
        // 
        this.tabConsentInfoRFI.Controls.Add(this.gvwRFIs);
        this.tabConsentInfoRFI.Controls.Add(this.lvwConsentInfoRFI);
        this.tabConsentInfoRFI.Location = new System.Drawing.Point(4, 32);
        this.tabConsentInfoRFI.Name = "tabConsentInfoRFI";
        this.tabConsentInfoRFI.Padding = new System.Windows.Forms.Padding(3);
        this.tabConsentInfoRFI.Size = new System.Drawing.Size(1084, 428);
        this.tabConsentInfoRFI.TabIndex = 4;
        this.tabConsentInfoRFI.Text = "RFI\'s";
        this.tabConsentInfoRFI.UseVisualStyleBackColor = true;
        // 
        // tabProcessingRFI
        // 
        this.tabProcessingRFI.Location = new System.Drawing.Point(4, 36);
        this.tabProcessingRFI.Name = "tabProcessingRFI";
        this.tabProcessingRFI.Padding = new System.Windows.Forms.Padding(3);
        this.tabProcessingRFI.Size = new System.Drawing.Size(868, 465);
        this.tabProcessingRFI.TabIndex = 1;
        this.tabProcessingRFI.Text = "RFI";
        this.tabProcessingRFI.UseVisualStyleBackColor = true;

您应该从选项卡中删除该对象。该对象不再位于选项卡的控件列表中。所以你可以这样做:

tabProcessingRFI.Controls.Remove(gvwRFIs);
gvwRFIs.Parent = tabConsentInfoRFI

您应该从选项卡中删除该对象。该对象不再位于选项卡的控件列表中。所以你可以这样做:

tabProcessingRFI.Controls.Remove(gvwRFIs);
gvwRFIs.Parent = tabConsentInfoRFI
我发现了问题, 同意设计器代码中的ListView是一个看起来相同但不再使用的旧控件。因此,当该控件被设置回原始区域选项卡的父级时,它就位于该控件的后台。 一旦控件被移除(虽然它已经被移除),代码就可以完美地工作

谢谢你让我找到了正确的方向。我找到了问题的答案, 同意设计器代码中的ListView是一个看起来相同但不再使用的旧控件。因此,当该控件被设置回原始区域选项卡的父级时,它就位于该控件的后台。 一旦控件被移除(虽然它已经被移除),代码就可以完美地工作


谢谢你让我找到了正确的方向。如果代码在另一个网格上工作,那么问题不在于发布的代码。请尝试查看可能导致此问题的任何事件处理程序。此DataGridView或其他DataGridView处理的唯一事件是双击事件。我将发布上面两个DataGridView的设计器代码。双击事件所做的就是打开另一个表单。如果代码在另一个网格上工作,那么问题就不会出在发布的代码上。请尝试查看可能导致此问题的任何事件处理程序。此DataGridView或其他DataGridView处理的唯一事件是双击事件。我将发布上面两个DataGridView的设计器代码。所有双击事件所做的就是打开另一个窗体。