djoshi
djoshi

Reputation: 363

How to add DataGridView dynamically to TabPage

I have a TabControl. I dynamically want to add TabPages that would have dynamically added DataGridView. I am able to add the tabPages dynamically but when I add the DataGridView to the dynamic tabPage nothing shows up. Appreciate any help that can be provided.

Here is the code.

                    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;

    }

Thanks

Upvotes: 3

Views: 15119

Answers (1)

Ted Spence
Ted Spence

Reputation: 2668

Have you tried moving the tab.Controls.Add(grid) statement to after the grid is configured?

Separately, I notice you are using "SuspendLayout()" to allow flicker free updates. Are you remembering to turn layout back on again?

For example, this:

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

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

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

Upvotes: 3

Related Questions