2012-07-23 139 views
2

我有一個TabControl。 我動態地想添加將動態添加DataGridView的TabPages。 我能夠動態添加tabPages,但是當我將DataGridView添加到動態tabPage時,什麼都沒有顯示出來。 欣賞任何可以提供的幫助。如何將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

請,不包括有關在問題的標題使用,除非它不會沒有很有意義語言信息。標籤用於此目的。 – 2014-03-05 12:06:34

+0

http://stackoverflow.com/questions/1084098/creating-a-tab-control-with-a-dynamic-number-of-tabs-in-visual-studio-c-sharp - 這可能會有所幫助。 – 2014-10-11 07:08:53

回答

1

您是否嘗試過移動tab.Controls.Add(網格)語句網格配置後?

另外,我注意到你正在使用「SuspendLayout()」允許無閃爍更新。你是否記得再次打開佈局?

例如,這樣的:

myTabPage.SuspendLayout(); 
tabControlNonQueued.TabPages.Add(myTabPage); 
DataGridView grid = new DataGridView(); 

// ... grid configuration and setup here ... 

tab.Controls.Add(grid); 
myTabPage.ResumeLayout(); 
tab.Refresh(); 
+0

我完成了您所建議的所有更改。我添加了ResumeLayout,並且刷新並將DataGridView添加到最後。它沒有幫助。 dataGridView不會顯示在動態添加的tabPages上。謝謝 – djoshi 2012-07-23 16:42:08

+0

爲了驗證,我添加了一個checkBox控件,並且添加並顯示正確。私人無效loadDataGridToTab(DataTable dt,TabPage選項卡,字符串tabName) { DataGridView grid = new DataGridView(); \t System.Windows.Forms.CheckBox cbSelectAll = new System.Windows.Forms.CheckBox(); tab.Controls.Add(cbSelectAll); tab.Controls.Add(grid); – djoshi 2012-07-23 17:10:39

+0

是否有可能將DataGridView添加到錯誤的父級?例如,選項卡控件包含許多內容:(1)選項卡控件本身,(2)頁面,(3)頁面中的子元素。我注意到這段代碼只是使用變量「tab」 - 是否有可能不是正確的元素? – 2012-07-23 17:37:27