【发布时间】:2012-07-23 16:11:24
【问题描述】:
我有一个 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;
}
谢谢
【问题讨论】:
-
请不要在问题标题中包含有关所用语言的信息,除非没有它就没有意义。标记用于此目的。
-
stackoverflow.com/questions/1084098/… - 这可能会有所帮助。
标签: c# windows datagridview tabcontrol tabpage