Winforms TabControl 中动态创建 tabPages 的问题
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
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 thetabControlMutants.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.
删除 dgw.AutoSize = true;
Remove
dgw.AutoSize = true;
尝试先添加控件,然后设置 Dock 属性
Try to add the control first, then set the Dock property