首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在TabPage中动态添加DataGridView

如何在TabPage中动态添加DataGridView
EN

Stack Overflow用户
提问于 2012-07-24 00:11:24
回答 1查看 14.4K关注 0票数 3

我有一台TabControl。我想动态地添加可以动态添加DataGridView的TabPages。我可以动态添加tabPages,但是当我将DataGridView添加到动态tabPage中时,没有显示任何内容。感谢任何可以提供的帮助。

下面是代码。

代码语言:javascript
复制
                    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;

    }

谢谢

EN

回答 1

Stack Overflow用户

发布于 2012-07-24 00:23:13

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

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

例如,如下所示:

代码语言:javascript
复制
myTabPage.SuspendLayout();
tabControlNonQueued.TabPages.Add(myTabPage);
DataGridView grid = new DataGridView();

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

tab.Controls.Add(grid);
myTabPage.ResumeLayout();
tab.Refresh();
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11616340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档