2014-10-11 38 views
0

我在用DataGridView實用地創建TabPage。 TabPage的顯示正確,但它們缺少DataGridView。任何人都可以辨別他們爲什麼沒有出現DGV標籤? LoadDataGridToTab格式正確。它被從另一個表格中調用。應該?在一個線程上創建的控件不能在不同線程上的控件上生成 - 將DataGridView添加到TabPage到TabControl

public void LoadDataGridToTab(Main main, string category) 
    { 
     try 
     { 
      //Set Cell Style 
      var dataGridViewCellStyle = new DataGridViewCellStyle 
      { 
       Alignment = DataGridViewContentAlignment.MiddleLeft, 
       BackColor = SystemColors.Control, 
       Font = new Font("Microsoft Sans Serif", 8.25F, 
        FontStyle.Regular, GraphicsUnit.Point, 0), 
       ForeColor = SystemColors.WindowText, 
       SelectionBackColor = SystemColors.Highlight, 
       SelectionForeColor = SystemColors.HighlightText, 
       WrapMode = DataGridViewTriState.True 
      }; 

      //Make the Grid 
      var grid = new DataGridView 
      { 
       Name = "dgv_" + category, 
       Text = category, 
       Visible = true, 
       Dock = DockStyle.Fill, 
       AllowUserToAddRows = false, 
       AllowUserToDeleteRows = false, 
       AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill, 
       ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize, 
       ColumnHeadersDefaultCellStyle = dataGridViewCellStyle, 
       DataSource = Auction.CacheAuctionsDataSet.Tables[category] 
      }; 

      //Binding 
      //var source = new BindingSource(); 
      //source.DataSource = CacheAuctionsDataSet.Tables[category]; 

      //Made the Tab 
      var tmpTabPage = new TabPage(category) 
      { 
       Name = "tabctrl_" + category, 
       Text = category, 
       Visible = true 
      }; 

      //Add the Grid to the Tab 
      tmpTabPage.Controls.Add(grid); 
      tmpTabPage.Refresh(); 

      //Add the Tab to the Control 
      tabctrl_Auctions.Controls.Add(tmpTabPage); 
      tabctrl_Auctions.Refresh(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.ToString()); 
     } 

    } 
+1

你從一個新的線程中調用這個函數呢? – JohnKiller 2014-10-11 09:44:52

+0

這是從非ui線程中調用的。我想我可能需要methodinvoke? – 2014-10-11 16:03:06

+1

是的。調用父表單 – JohnKiller 2014-10-11 16:04:36

回答

相關問題