我有一个TabControl。 我动态地想要添加动态添加DataGridView的TabPages。 我可以动态添加tabPages,但是当我将DataGridView添加到动态tabPage时,没有任何显示。 感谢可以提供的任何帮助。
这是代码。
myTabPage.SuspendLayout();
tabControlNonQueued.TabPages.Add(myTabPage);
loadDataGridToTab(dataTable, myTabPage);
private void loadDataGridToTab(DataTable dt, TabPage tab)
{
DataGridView grid = new DataGridView();
tab.Controls.Add(grid);
tab.Refresh();
grid.Visible = true;
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle = new System.Windows.Forms.DataGridViewCellStyle();
grid.AllowUserToAddRows = false;
grid.AllowUserToDeleteRows = false;
grid.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
grid.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle;
grid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridViewCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
grid.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle;
grid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
//grid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
//this.cbDG});
hideDGColumn(grid, "Counter");
SetFontAndColors(grid);
lockDataGrid(grid);
BindingSource source = new BindingSource();
source.DataSource = dt;
grid.Dock = DockStyle.Fill;
grid.DataSource = source;
}
由于
答案 0 :(得分:2)
您是否尝试过将tab.Controls.Add(grid)语句移动到网格配置后?
另外,我注意到你正在使用" SuspendLayout()"允许无闪烁更新。你还记得重新打开布局吗?
例如,这个:
myTabPage.SuspendLayout();
tabControlNonQueued.TabPages.Add(myTabPage);
DataGridView grid = new DataGridView();
// ... grid configuration and setup here ...
tab.Controls.Add(grid);
myTabPage.ResumeLayout();
tab.Refresh();