views:

463

answers:

3

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?

+1  A: 

Remove dgw.AutoSize = true;

Catalin DICU
it does not solve the problem
mirt
A: 

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.

Henk Holterman
Yes, you are right, the error is somewhere else, namely in main form constructor i invoke SuspendLayout method (I don't know why), that suspends the layout logic for this form. When i remove this method everything started to work. Thenks for help :)
mirt
A: 

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

Seb