mirt
mirt

Reputation: 202

Problem with dynamic create tabPages in Winforms TabControl

I want to create dynamic tabPages in TabControl. In each tabPage I create dataGridView and i want to fill the entire space of each tabPage with this dataGrid. Here is code, where i do this:

private void tabControlMutants_SelectedIndexChanged(object sender, EventArgs e)
{
    DataGridView dgw = new DataGridView(); 

    DataGridViewTextBoxColumn testCaseCol = new System.Windows.Forms.DataGridViewTextBoxColumn();
    DataGridViewTextBoxColumn resultCol = new System.Windoows.Forms.DataGridViewTextBoxColumn();
    // 
    // dataGridView1
    // 
    dgw.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    dgw.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
    testCaseCol,
    resultCol});
    dgw.Location = new System.Drawing.Point(3, 3);
    dgw.Name = "dataGridView1";
    dgw.AutoSize = true;
    dgw.Dock = System.Windows.Forms.DockStyle.Fill;
    dgw.TabIndex = 0;
    // 
    // TestCaseColumn
    // 
    testCaseCol.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
    testCaseCol.HeaderText = "Test Case";
    testCaseCol.Name = "TestCaseColumn";
    // 
    // ResultColumn
    // 
    resultCol.HeaderText = "Result";
    resultCol.Name = "ResultColumn";

    tabControlMutants.TabPages[(sender as TabControl).SelectedIndex].Controls.Add(dgw);
    ((System.ComponentModel.ISupportInitialize)(dgw)).EndInit();

    //fill dataGridView

}

But it doesn't work, becouse when i resize the main window, data gridView doesn.t change its size (although the dock property is set to fill). Any ideas?

Upvotes: 4

Views: 2992

Answers (3)

Henk Holterman
Henk Holterman

Reputation: 273179

Move the dgw.Dock = System.Windows.Forms.DockStyle.Fill; statement to below the tabControlMutants.TabPages[...].Controls.Add(dgw);line. And maybe below the EndInit(), I'm not sure.

And remove the dgw.Location = ... line because its not needed.

Edit:

I just ran a little test and this basically should work. That means the error is somewhere else, in the code not shown. Maybe in the 'fill rows part'.
I recommend you start removing parts of the code to eliminate the error.

And you do realize you create a Dgv each time a tab is selected, do you? I assume this is demo code.

Upvotes: 1

Seb
Seb

Reputation: 2715

Try to add the control first, then set the Dock property

Upvotes: 1

Catalin DICU
Catalin DICU

Reputation: 4638

Remove dgw.AutoSize = true;

Upvotes: 1

Related Questions