Winforms TabControl 中动态创建 tabPages 的问题

发布于 2024-08-26 16:32:00 字数 1474 浏览 7 评论 0原文

我想在 TabControl 中创建动态 tabPages。在每个 tabPage 中,我创建 dataGridView 并且我想用这个 dataGrid 填充每个 tabPage 的整个空间。这是代码,我在其中执行此操作:

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

}

但它不起作用,因为当我调整主窗口大小时,数据 gridView 不会更改其大小(尽管将停靠属性设置为填充)。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不羁少年 2024-09-02 16:32:00

dgw.Dock = System.Windows.Forms.DockStyle.Fill; 语句移至 tabControlMutants.TabPages[...].Controls.Add(下方 dgw);行。也许在 EndInit() 下面,我不确定。

并删除 dgw.Location = ... 行,因为不需要它。

编辑:

我刚刚进行了一些测试,这基本上应该可以工作。这意味着错误在其他地方,在未显示的代码中。也许在“填充行部分”。
我建议您开始删除部分代码以消除错误。

您确实意识到每次选择选项卡时都会创建一个 Dgv,是吗?我认为这是演示代码。

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.

牛↙奶布丁 2024-09-02 16:32:00

删除 dgw.AutoSize = true;

Remove dgw.AutoSize = true;

三生路 2024-09-02 16:32:00

尝试先添加控件,然后设置 Dock 属性

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文