【问题标题】:How to add DataGridView dynamically to TabPage如何将 DataGridView 动态添加到 TabPage
【发布时间】:2012-07-23 16:11:24
【问题描述】:

我有一个 TabControl。 我动态地想要添加将动态添加 DataGridView 的 TabPages。 我可以动态添加 tabPages,但是当我将 DataGridView 添加到动态 tabPage 时,什么都没有显示。 感谢您提供的任何帮助。

这里是代码。

                    myTabPage.SuspendLayout();
                    tabControlNonQueued.TabPages.Add(myTabPage);
                    loadDataGridToTab(dataTable, myTabPage);
    private void loadDataGridToTab(DataTable dt, TabPage tab)
    {
        DataGridView grid = new DataGridView();
        tab.Controls.Add(grid);
        tab.Refresh();
        grid.Visible = true;
        System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle = new System.Windows.Forms.DataGridViewCellStyle();
        grid.AllowUserToAddRows = false;
        grid.AllowUserToDeleteRows = false;
        grid.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
        grid.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle;
        grid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        dataGridViewCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
        dataGridViewCellStyle.BackColor = System.Drawing.SystemColors.Control;
        dataGridViewCellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        dataGridViewCellStyle.ForeColor = System.Drawing.SystemColors.WindowText;
        dataGridViewCellStyle.SelectionBackColor = System.Drawing.SystemColors.Highlight;
        dataGridViewCellStyle.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
        dataGridViewCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
        grid.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle;
        grid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        //grid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        //this.cbDG});


        hideDGColumn(grid, "Counter");
        SetFontAndColors(grid);
        lockDataGrid(grid);
        BindingSource source = new BindingSource();
        source.DataSource = dt;
        grid.Dock = DockStyle.Fill;
        grid.DataSource = source;

    }

谢谢

【问题讨论】:

标签: c# windows datagridview tabcontrol tabpage


【解决方案1】:

您是否尝试过将 tab.Controls.Add(grid) 语句移到配置网格之后?

另外,我注意到您正在使用“SuspendLayout()”来允许无闪烁更新。您还记得重新打开布局吗?

例如,这个:

myTabPage.SuspendLayout();
tabControlNonQueued.TabPages.Add(myTabPage);
DataGridView grid = new DataGridView();

// ... grid configuration and setup here ...

tab.Controls.Add(grid);
myTabPage.ResumeLayout();
tab.Refresh();

【讨论】:

  • 我做了你建议的所有改变。我添加了 ResumeLayout 并刷新并将 DataGridView 添加到最后。它没有帮助。 dataGridView 不会显示在动态添加的 tabPages 上。谢谢
  • 只是为了验证,我添加了一个复选框控件,它被添加并显示正确。私有 void loadDataGridToTab(DataTable dt, TabPage tab,string tabName) { DataGridView grid = new DataGridView(); System.Windows.Forms.CheckBox cbSelectAll = new System.Windows.Forms.CheckBox(); tab.Controls.Add(cbSelectAll); tab.Controls.Add(grid);
  • 您是否有可能将 DataGridView 添加到错误的父级?例如,一个选项卡控件由许多东西组成:(1) 选项卡控件本身,(2) 页面,(3) 页面中的子元素。我注意到这段代码只使用了变量“tab”——这可能不是正确的元素吗?
  • 另外,请尝试阅读以下内容:stackoverflow.com/questions/2678184/… - 在显示标签页之前,您可能无法添加元素。您可能需要将代码移动到“TabPageSelected”事件中。
  • 我必须将其设置为可见。即使我在添加到选项卡之前将其设置为可见。一旦添加到选项卡中,我将它设置为可见,现在 GridView 显示。 tab.Refresh(); grid.Visible = true;谢谢泰德。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多