Piper
Piper

Reputation: 55

How to create DataGridsViews on tabs that are being auto generated

When loading my excel file with many sheets, I'm taking the name of each sheet and having tabs in a tabControl automatically generate. This works. Next I'm having trouble making it so a new and different dataGridView gets generated on each tab. Here is my code so far for this part

foreach (DataRow row in dt1.Rows) {
  comboBox1.Items.Add(row["TABLE_NAME"].ToString());
  tabControl2.TabPages.Add(row["TABLE_NAME"].ToString());

  DataGridView grid = new DataGridView();
  TabPages.Controls.Add(grid);  // red line under TabPages  **********
}

Upvotes: 0

Views: 329

Answers (1)

Neil Smith
Neil Smith

Reputation: 2565

Set aside the new TabPage so you can add controls to it:

foreach (DataRow row in dt1.Rows) {
    string name = row["TABLE_NAME"].ToString();
    var tabPage = new TabPage(name);
    var grid = new DataGridView();

    tabPage.Controls.Add(grid);
    comboBox1.Items.Add(name);        
    tabControl2.TabPages.Add(tapPage);
}

Upvotes: 1

Related Questions