![](/img/trans.png)
[英]How to access dynamically added DataGridView inside a TabPage and get values?
[英]How to add DataGridView dynamically to TabPage
我有一個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;
}
謝謝
您是否嘗試過將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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.