开发者

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();开发者_如何学C

    //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?


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.


Remove dgw.AutoSize = true;


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜