【问题标题】:Changing Contol Parent Removes Content更改 Control Parent 会删除内容
【发布时间】:2012-09-23 22:38:05
【问题描述】:

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

我通过更改其父级来做到这一点。这在第一次从原始选项卡控件移动到新选项卡控件时没有问题,但是当将父级更改回原始选项卡时,DataGridView 显示(所有列都可见)但视图中没有数据。

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

我也在为另一个 DataGridView 使用这个确切的代码,它完全没有任何问题。

任何想法都将不胜感激,并在此先感谢。

从原始到次要

gvwRFIs.Parent = tabProcessingRFI; //Working
gvwConsentInfoMemos.Parent = tabProcessingMemos; //Working

从次要到原始

gvwRFIs.Parent = tabConsentInfoRFI; //Empty Data
gvwConsentInfoMemos.Parent = tabConsentInfoMemos; //Working

RFI DataGridView 设计器代码

        // 
        // 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;

【问题讨论】:

  • 如果代码在另一个网格上工作,那么问题不会出在发布的代码上。尝试查看任何可能导致问题的事件处理程序。
  • 这个或其他 DataGridView 处理的唯一事件是双击事件。我将发布上面两个 DataGridViews 的设计器代码。双击事件所做的就是打开另一个表单。

标签: c# winforms datagridview parent tabcontrol


【解决方案1】:

我发现了问题, 许可设计器代码中的 ListView 是一个看起来相同但不再使用的旧控件。因此,当控件的父级返回到“原始”选项卡时,它位于该控件的背景中。 一旦控件被移除(认为它已经被移除),代码就会完美运行。

感谢 LarsTech 让我朝着正确的方向前进。以及 akhisp 的答案。

【讨论】:

    【解决方案2】:

    您应该从选项卡的 Tab.Controls 列表中删除不再存在的对象。所以请执行以下操作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      相关资源
      最近更新 更多