Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheet

Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheet

Scheduled Pinned Locked Moved C#
comhelpquestion
18 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Walaza

    Hi, Thanks for the reply. Should I have put this code in the constructor or should I put it under the button that loads the spreadsheet? Should I put the tabControl from the ToolBox or should I only use the dynamically created one? I have put in your suggested code but when I load the spreadsheet my form just becomes blank and the tabs do not get created. :doh:

    Mvelo Walaza Developer Telkom SA

    L Offline
    L Offline
    lsconyer
    wrote on last edited by
    #5

    I would put this code under the button that loads the spreadsheet. If not, you'll get a problem. And I need to modify the code I submitted. In the code I had you create a new TabControl without adding it to the form. Hopefully, you already have a tab control on the page so the designer can set a bunch of properties. I'm going to modify my original code based on that assumption. If not, you'll have to do something like this.Controls.Add(tabControl1);

    Lester http://www.lestersconyers.com

    W 1 Reply Last reply
    0
    • L lsconyer

      I would put this code under the button that loads the spreadsheet. If not, you'll get a problem. And I need to modify the code I submitted. In the code I had you create a new TabControl without adding it to the form. Hopefully, you already have a tab control on the page so the designer can set a bunch of properties. I'm going to modify my original code based on that assumption. If not, you'll have to do something like this.Controls.Add(tabControl1);

      Lester http://www.lestersconyers.com

      W Offline
      W Offline
      Walaza
      wrote on last edited by
      #6

      Thank you so much, I am making so much progress in one day! The problem is, it only shows the data of only one worksheet. It shows the tabs and their worksheets names but does not add the datagrid and the data to the tabpage. What could I be doing wrong, here's the code: //This takes the Excel file from a specific location //and displays it in the datagrid private void btnOpenFile_Click(object sender, EventArgs e) { // prepare open file dialog to only search for excel files this.ofd.FileName = "*.xls"; if (this.ofd.ShowDialog() == DialogResult.OK) { try { // Here is the call to Open a Workbook in Excel // It uses most of the default values (except for the read-only which we set to true) Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, true, true); // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets; //theWorkbook.Worksheets[sheets]; // get the first and only worksheet from the collection of worksheets Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1); TabControl tbControl = new TabControl(); this.Controls.Add(tbControl); tbControl.Dock = DockStyle.Bottom; tbControl.Height = 660; TabPage page; //DataGridView dgv; foreach (Worksheet sheet in sheets) { page = new TabPage(sheet.Name.ToString()); //dgOutput = new DataGridView(); //get start and end rows Object[] startRow = { worksheet.get_Range("A4", "O4") }; int endRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; //Check the columns...Removed // loop through the rows of the spreadsheet and place each row in the datagrid for (int i = 5; i <= endRo

      L 1 Reply Last reply
      0
      • W Walaza

        Thank you so much, I am making so much progress in one day! The problem is, it only shows the data of only one worksheet. It shows the tabs and their worksheets names but does not add the datagrid and the data to the tabpage. What could I be doing wrong, here's the code: //This takes the Excel file from a specific location //and displays it in the datagrid private void btnOpenFile_Click(object sender, EventArgs e) { // prepare open file dialog to only search for excel files this.ofd.FileName = "*.xls"; if (this.ofd.ShowDialog() == DialogResult.OK) { try { // Here is the call to Open a Workbook in Excel // It uses most of the default values (except for the read-only which we set to true) Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(ofd.FileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, true, true); // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets; //theWorkbook.Worksheets[sheets]; // get the first and only worksheet from the collection of worksheets Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1); TabControl tbControl = new TabControl(); this.Controls.Add(tbControl); tbControl.Dock = DockStyle.Bottom; tbControl.Height = 660; TabPage page; //DataGridView dgv; foreach (Worksheet sheet in sheets) { page = new TabPage(sheet.Name.ToString()); //dgOutput = new DataGridView(); //get start and end rows Object[] startRow = { worksheet.get_Range("A4", "O4") }; int endRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; //Check the columns...Removed // loop through the rows of the spreadsheet and place each row in the datagrid for (int i = 5; i <= endRo

        L Offline
        L Offline
        lsconyer
        wrote on last edited by
        #7

        You have to create a new datagridview object for each new page. Then add that datagridview to the page. Now, you're just adding dgOutput to each page. Then reseting it and adding it to the next page. It'll be on the last page you inserted, which will be the first page when you .Insert(0, page). Uncomment the //dgOutput = new DataGridView();

        Lester http://www.lestersconyers.com

        W 1 Reply Last reply
        0
        • L lsconyer

          You have to create a new datagridview object for each new page. Then add that datagridview to the page. Now, you're just adding dgOutput to each page. Then reseting it and adding it to the next page. It'll be on the last page you inserted, which will be the first page when you .Insert(0, page). Uncomment the //dgOutput = new DataGridView();

          Lester http://www.lestersconyers.com

          W Offline
          W Offline
          Walaza
          wrote on last edited by
          #8

          When I uncomment the line: dgOutput = new DataGridView(); I get the ff error: 'No row can be added to a DataGridView control that does not have columns. Columns must be added first.' Is there anything else I need to add to the code below: TabControl tbControl = new TabControl(); this.Controls.Add(tbControl); tbControl.Dock = DockStyle.Bottom; tbControl.Height = 660; TabPage page; foreach (Worksheet sheet in sheets) { page = new TabPage(sheet.Name.ToString()); dgOutput = new DataGridView(); //get start and end rows Object[] startRow = { worksheet.get_Range("A4", "O4") }; int endRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; //Check if all columns are according to spec if ((worksheet.get_Range("A3", "A3").Text.ToString() == "Template") && (worksheet.get_Range("B3", "B3").Text.ToString() == "Error Code") && (worksheet.get_Range("C3", "C3").Text.ToString() == "Error Name") && (worksheet.get_Range("D3", "D3").Text.ToString() == "Error Description") && (worksheet.get_Range("E3", "E3").Text.ToString() == "Error Cause") && (worksheet.get_Range("F3", "F3").Text.ToString() == "Source") && (worksheet.get_Range("G3", "G3").Text.ToString() == "Destination") && (worksheet.get_Range("H3", "H3").Text.ToString() == "Category") && (worksheet.get_Range("I3", "I3").Text.ToString() == "Severity") && (worksheet.get_Range("J3", "J3").Text.ToString() == "Threshold") && (worksheet.get_Range("K3", "K3").Text.ToString() == "Responsible for Resolution") && (worksheet.get_Range("L3", "L3").Text.ToString() == "Action") && (worksheet.get_Range("M3", "M3").Text.ToString() == "ECS Resubmit Y/N") && (worksheet.get_Range("N3", "N3").Text.ToString() == "ECS Auto Resubmit Y/N") &&

          L 2 Replies Last reply
          0
          • W Walaza

            When I uncomment the line: dgOutput = new DataGridView(); I get the ff error: 'No row can be added to a DataGridView control that does not have columns. Columns must be added first.' Is there anything else I need to add to the code below: TabControl tbControl = new TabControl(); this.Controls.Add(tbControl); tbControl.Dock = DockStyle.Bottom; tbControl.Height = 660; TabPage page; foreach (Worksheet sheet in sheets) { page = new TabPage(sheet.Name.ToString()); dgOutput = new DataGridView(); //get start and end rows Object[] startRow = { worksheet.get_Range("A4", "O4") }; int endRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; //Check if all columns are according to spec if ((worksheet.get_Range("A3", "A3").Text.ToString() == "Template") && (worksheet.get_Range("B3", "B3").Text.ToString() == "Error Code") && (worksheet.get_Range("C3", "C3").Text.ToString() == "Error Name") && (worksheet.get_Range("D3", "D3").Text.ToString() == "Error Description") && (worksheet.get_Range("E3", "E3").Text.ToString() == "Error Cause") && (worksheet.get_Range("F3", "F3").Text.ToString() == "Source") && (worksheet.get_Range("G3", "G3").Text.ToString() == "Destination") && (worksheet.get_Range("H3", "H3").Text.ToString() == "Category") && (worksheet.get_Range("I3", "I3").Text.ToString() == "Severity") && (worksheet.get_Range("J3", "J3").Text.ToString() == "Threshold") && (worksheet.get_Range("K3", "K3").Text.ToString() == "Responsible for Resolution") && (worksheet.get_Range("L3", "L3").Text.ToString() == "Action") && (worksheet.get_Range("M3", "M3").Text.ToString() == "ECS Resubmit Y/N") && (worksheet.get_Range("N3", "N3").Text.ToString() == "ECS Auto Resubmit Y/N") &&

            L Offline
            L Offline
            lsconyer
            wrote on last edited by
            #9

            There's nothing else you can do. You have to create a new datagridview for each tab. Within the foreach loop, set up the columns for the datagridview. You can't use a global datagridview. Within that foreach loop, do everything you need to do for a datagridview then add it to the tabpage.

            Lester http://www.lestersconyers.com

            W 1 Reply Last reply
            0
            • L lsconyer

              There's nothing else you can do. You have to create a new datagridview for each tab. Within the foreach loop, set up the columns for the datagridview. You can't use a global datagridview. Within that foreach loop, do everything you need to do for a datagridview then add it to the tabpage.

              Lester http://www.lestersconyers.com

              W Offline
              W Offline
              Walaza
              wrote on last edited by
              #10

              I did as you told me and the datagrid is visible in each tabpage but now the problem is that it only shows the data that is in the first sheet in all the tabs. Each tabpage should have its own data and should display its own worksheet in the datagrid.

              Mvelo Walaza Developer Telkom SA

              1 Reply Last reply
              0
              • W Walaza

                When I uncomment the line: dgOutput = new DataGridView(); I get the ff error: 'No row can be added to a DataGridView control that does not have columns. Columns must be added first.' Is there anything else I need to add to the code below: TabControl tbControl = new TabControl(); this.Controls.Add(tbControl); tbControl.Dock = DockStyle.Bottom; tbControl.Height = 660; TabPage page; foreach (Worksheet sheet in sheets) { page = new TabPage(sheet.Name.ToString()); dgOutput = new DataGridView(); //get start and end rows Object[] startRow = { worksheet.get_Range("A4", "O4") }; int endRow = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; //Check if all columns are according to spec if ((worksheet.get_Range("A3", "A3").Text.ToString() == "Template") && (worksheet.get_Range("B3", "B3").Text.ToString() == "Error Code") && (worksheet.get_Range("C3", "C3").Text.ToString() == "Error Name") && (worksheet.get_Range("D3", "D3").Text.ToString() == "Error Description") && (worksheet.get_Range("E3", "E3").Text.ToString() == "Error Cause") && (worksheet.get_Range("F3", "F3").Text.ToString() == "Source") && (worksheet.get_Range("G3", "G3").Text.ToString() == "Destination") && (worksheet.get_Range("H3", "H3").Text.ToString() == "Category") && (worksheet.get_Range("I3", "I3").Text.ToString() == "Severity") && (worksheet.get_Range("J3", "J3").Text.ToString() == "Threshold") && (worksheet.get_Range("K3", "K3").Text.ToString() == "Responsible for Resolution") && (worksheet.get_Range("L3", "L3").Text.ToString() == "Action") && (worksheet.get_Range("M3", "M3").Text.ToString() == "ECS Resubmit Y/N") && (worksheet.get_Range("N3", "N3").Text.ToString() == "ECS Auto Resubmit Y/N") &&

                L Offline
                L Offline
                lsconyer
                wrote on last edited by
                #11

                Where does that variable named worksheet come from? Instead of using worksheet to check validity and get range, you should be using the variable name sheet. Replace the variable named worksheet with the variable named sheet inside of the foreach loop.

                Lester http://www.lestersconyers.com

                W 2 Replies Last reply
                0
                • L lsconyer

                  Where does that variable named worksheet come from? Instead of using worksheet to check validity and get range, you should be using the variable name sheet. Replace the variable named worksheet with the variable named sheet inside of the foreach loop.

                  Lester http://www.lestersconyers.com

                  W Offline
                  W Offline
                  Walaza
                  wrote on last edited by
                  #12

                  Thank you very much, it worked!!

                  Mvelo Walaza Developer Telkom SA

                  1 Reply Last reply
                  0
                  • L lsconyer

                    Where does that variable named worksheet come from? Instead of using worksheet to check validity and get range, you should be using the variable name sheet. Replace the variable named worksheet with the variable named sheet inside of the foreach loop.

                    Lester http://www.lestersconyers.com

                    W Offline
                    W Offline
                    Walaza
                    wrote on last edited by
                    #13

                    Hi, Would you happen to know how I could take the data from all the dynamically created tabpages’ datagrids and export the data to excel and create a new worksheet for each tabpage as it is exporting. Currently it takes data from one dynamically created tabpage (the default)‘s datagrid and exports it to a spreadsheet. Here's the code I have so far: Microsoft.Office.Interop.Excel._Application _appClass = new ApplicationClass(); _appClass.Visible = true; _appClass.Caption = "Exported ECS Errors"; Microsoft.Office.Interop.Excel.Workbook wbk = _appClass.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet obj = (Worksheet)wbk.ActiveSheet; // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = wbk.Worksheets; Range r = (Range)obj.Cells; try { //Load the Headers and make them BOLD for (int i = 0; i < ((DataGridView)this.dgOutput).Columns.Count; i++) { r[1, i + 1] = ((DataGridView)this.dgOutput).Columns[i].HeaderText.ToString(); ((Range)r.Cells[1, i + 1]).Font.Bold = true; } //Load the data from the datagrid to the new spreadsheet for (int i = 1; i < ((DataGridView)this.dgOutput).Rows.Count - 1; i++) { for (int j = 0; j < ((DataGridView)this.dgOutput).Columns.Count; j++) { r[i + 1, j + 1] = ((DataGridView)this.dgOutput).Rows[i].Cells[j].Value.ToString(); //Do the colors if (((DataGridView)this.dgOutput).Rows[i].Cells[j].Value.ToString() == "Update OK") { ((Range)r.Cells[i + 1, 1]).Font.Color = Color.Green.G; } } } } catch (Exception EX) //user closes the Excel file while it is loading data from the datagrid { MessageBox.Show(EX.Message + "The Excel file was closed while loading data from the datagrid.", "Error : Loading file!!", MessageBoxButtons.OK, MessageB

                    L 1 Reply Last reply
                    0
                    • W Walaza

                      Hi, Would you happen to know how I could take the data from all the dynamically created tabpages’ datagrids and export the data to excel and create a new worksheet for each tabpage as it is exporting. Currently it takes data from one dynamically created tabpage (the default)‘s datagrid and exports it to a spreadsheet. Here's the code I have so far: Microsoft.Office.Interop.Excel._Application _appClass = new ApplicationClass(); _appClass.Visible = true; _appClass.Caption = "Exported ECS Errors"; Microsoft.Office.Interop.Excel.Workbook wbk = _appClass.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet obj = (Worksheet)wbk.ActiveSheet; // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = wbk.Worksheets; Range r = (Range)obj.Cells; try { //Load the Headers and make them BOLD for (int i = 0; i < ((DataGridView)this.dgOutput).Columns.Count; i++) { r[1, i + 1] = ((DataGridView)this.dgOutput).Columns[i].HeaderText.ToString(); ((Range)r.Cells[1, i + 1]).Font.Bold = true; } //Load the data from the datagrid to the new spreadsheet for (int i = 1; i < ((DataGridView)this.dgOutput).Rows.Count - 1; i++) { for (int j = 0; j < ((DataGridView)this.dgOutput).Columns.Count; j++) { r[i + 1, j + 1] = ((DataGridView)this.dgOutput).Rows[i].Cells[j].Value.ToString(); //Do the colors if (((DataGridView)this.dgOutput).Rows[i].Cells[j].Value.ToString() == "Update OK") { ((Range)r.Cells[i + 1, 1]).Font.Color = Color.Green.G; } } } } catch (Exception EX) //user closes the Excel file while it is loading data from the datagrid { MessageBox.Show(EX.Message + "The Excel file was closed while loading data from the datagrid.", "Error : Loading file!!", MessageBoxButtons.OK, MessageB

                      L Offline
                      L Offline
                      lsconyer
                      wrote on last edited by
                      #14

                      I don't see why not. You just have to loop through the tabpages. You have to know your logic. I don't have time to work on the code but here is what should work: //loop through each tabpage in tabcontrol.tabpages foreach(TabPage page in tabcontrol.TabPages) { //get the datagrid from the tabpage's control. It may not be at the 0 index but it should be in there DataGridView dgv = (DataGridView)page.Controls[0]; //loop through each datarow in datagridview foreach(DataGridViewRow row in dgv.Rows) { //create new worksheet //load data from datagrid to worksheet //add worksheet to workbook } } Good luck

                      Lester http://www.lestersconyers.com

                      modified on Wednesday, April 9, 2008 9:40 AM

                      W 2 Replies Last reply
                      0
                      • L lsconyer

                        I don't see why not. You just have to loop through the tabpages. You have to know your logic. I don't have time to work on the code but here is what should work: //loop through each tabpage in tabcontrol.tabpages foreach(TabPage page in tabcontrol.TabPages) { //get the datagrid from the tabpage's control. It may not be at the 0 index but it should be in there DataGridView dgv = (DataGridView)page.Controls[0]; //loop through each datarow in datagridview foreach(DataGridViewRow row in dgv.Rows) { //create new worksheet //load data from datagrid to worksheet //add worksheet to workbook } } Good luck

                        Lester http://www.lestersconyers.com

                        modified on Wednesday, April 9, 2008 9:40 AM

                        W Offline
                        W Offline
                        Walaza
                        wrote on last edited by
                        #15

                        Hi, These are the errors I get when I try the code you suggested: 1. foreach statement cannot operate on variables of type 'System.Windows.Forms.TabControl' because 'System.Windows.Forms.TabControl' does not contain a public definition for 'GetEnumerator' 2. foreach statement cannot operate on variables of type 'System.Windows.Forms.DataGridView' because 'System.Windows.Forms.DataGridView' does not contain a public definition for 'GetEnumerator' Is there anything I am missing?

                        Mvelo Walaza Developer Telkom SA

                        L 1 Reply Last reply
                        0
                        • W Walaza

                          Hi, These are the errors I get when I try the code you suggested: 1. foreach statement cannot operate on variables of type 'System.Windows.Forms.TabControl' because 'System.Windows.Forms.TabControl' does not contain a public definition for 'GetEnumerator' 2. foreach statement cannot operate on variables of type 'System.Windows.Forms.DataGridView' because 'System.Windows.Forms.DataGridView' does not contain a public definition for 'GetEnumerator' Is there anything I am missing?

                          Mvelo Walaza Developer Telkom SA

                          L Offline
                          L Offline
                          lsconyer
                          wrote on last edited by
                          #16

                          I edited the code. Try it.

                          Lester http://www.lestersconyers.com

                          1 Reply Last reply
                          0
                          • L lsconyer

                            I don't see why not. You just have to loop through the tabpages. You have to know your logic. I don't have time to work on the code but here is what should work: //loop through each tabpage in tabcontrol.tabpages foreach(TabPage page in tabcontrol.TabPages) { //get the datagrid from the tabpage's control. It may not be at the 0 index but it should be in there DataGridView dgv = (DataGridView)page.Controls[0]; //loop through each datarow in datagridview foreach(DataGridViewRow row in dgv.Rows) { //create new worksheet //load data from datagrid to worksheet //add worksheet to workbook } } Good luck

                            Lester http://www.lestersconyers.com

                            modified on Wednesday, April 9, 2008 9:40 AM

                            W Offline
                            W Offline
                            Walaza
                            wrote on last edited by
                            #17

                            I tried the code but when I click the button nothing happens. What could I be doing wrong? Here is the code: //This exports data from the datagrid to a new EXCEL spreadsheet private void btnExport_Click(object sender, EventArgs e) { //Check if the file has been added to the datagrid if (dgOutput.RowCount == 1) { MessageBox.Show("Please load the file first!!", "Error : Load file!!", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { TabControl tbControl = new TabControl(); //loop through each tabpage in tabcontrol.tabpages foreach (TabPage page in tbControl.TabPages) { //get the datagrid from the tabpage's control. It may not be at the 0 index but it should be in there DataGridView dgv = (DataGridView)page.Controls[0]; //loop through each datarow in datagridview foreach (DataGridViewRow row in dgv.Rows) { //create new worksheet Microsoft.Office.Interop.Excel._Application _appClass = new ApplicationClass(); _appClass.Visible = true; _appClass.Caption = "Exported ECS Errors"; Microsoft.Office.Interop.Excel.Workbook wbk = _appClass.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet obj = (Worksheet)wbk.ActiveSheet; // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = wbk.Worksheets; Range r = (Range)obj.Cells; //_appClass.Workbooks.Add(obj); //load data from datagrid to worksheet try { //Load the Headers and make them BOLD for (int i = 0; i < ((DataGridView)this.dgOutput).Columns.Count; i++) { r[1, i + 1] = ((DataGridView)this.dgOutput).Columns[i].HeaderText.ToString(); ((Range)r.Cells[1, i + 1]).Font.Bold = true; } //Load the data from the datagrid to the new spreadsheet

                            L 1 Reply Last reply
                            0
                            • W Walaza

                              I tried the code but when I click the button nothing happens. What could I be doing wrong? Here is the code: //This exports data from the datagrid to a new EXCEL spreadsheet private void btnExport_Click(object sender, EventArgs e) { //Check if the file has been added to the datagrid if (dgOutput.RowCount == 1) { MessageBox.Show("Please load the file first!!", "Error : Load file!!", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { TabControl tbControl = new TabControl(); //loop through each tabpage in tabcontrol.tabpages foreach (TabPage page in tbControl.TabPages) { //get the datagrid from the tabpage's control. It may not be at the 0 index but it should be in there DataGridView dgv = (DataGridView)page.Controls[0]; //loop through each datarow in datagridview foreach (DataGridViewRow row in dgv.Rows) { //create new worksheet Microsoft.Office.Interop.Excel._Application _appClass = new ApplicationClass(); _appClass.Visible = true; _appClass.Caption = "Exported ECS Errors"; Microsoft.Office.Interop.Excel.Workbook wbk = _appClass.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet obj = (Worksheet)wbk.ActiveSheet; // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = wbk.Worksheets; Range r = (Range)obj.Cells; //_appClass.Workbooks.Add(obj); //load data from datagrid to worksheet try { //Load the Headers and make them BOLD for (int i = 0; i < ((DataGridView)this.dgOutput).Columns.Count; i++) { r[1, i + 1] = ((DataGridView)this.dgOutput).Columns[i].HeaderText.ToString(); ((Range)r.Cells[1, i + 1]).Font.Bold = true; } //Load the data from the datagrid to the new spreadsheet

                              L Offline
                              L Offline
                              lsconyer
                              wrote on last edited by
                              #18

                              Just about your whole code needs to be rewritten. I think you're making this harder on yourself than you needed to. The reason nothing is happening is because tbControl is a new tabcontrol with no tabpages. Instead of instantiating a new tabcontrol, you need to loop through the tabpages of the original tabcontrol that you were working with at the beginning. To save yourself some trouble, let the designer work for you. Simply drag and drop a tabcontrol on the page. Now, it can be accessed globally. Also, ditch that global datagridview named dgOutput. You don't need it and it shouldn't be being used. It shouldn't be being used because you create a new datagridview for each tab page that's created dynamically. Once you get all that rewired, take a look at your code. Think about exactly what's going to happen. Right now, if that foreach loop would execute, you would only be getting data from this.dgOutput but your looping through the rows of dgv which is the datagridview on each tabpage. You want to loop through the rows of dgv to get values. You want to loop through the columns of dgv to load the headers. Then, after the headers and values are in the worksheet, you want to add it to the workbook. Set some breakpoints within your loops and the rest of your code to debug. It'll save you alot of time and you'll learn a whole lot quicker than somebody helping you.

                              Lester http://www.lestersconyers.com

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups