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.
lsconyer
Posts
-
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 spreadsheetI edited the code. Try it.
-
Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheetI 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 luckLester http://www.lestersconyers.com
modified on Wednesday, April 9, 2008 9:40 AM
-
SaveFileDialog filterHello all. I'm trying to allow a user to save a text file and only a text file. My filter is "Text files (*txt)|*txt" This seems to be set up fine but when I save a file, I can save as .doc. This shouldn't happen, at least that's what I thought. Where am I going wrong?
-
Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheetWhere 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.
-
Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheetThere'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.
-
Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheetYou 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();
-
Event handler for menu populated from databaseI've never heard of a the Menu class. I've heard of MainMenu, MenuStrip, ContextMenu, ToolStrip, but not that. Anyway, if its anything like the previous three components, you can capture the click events on the items themselves like someone already mentioned. Or you can handle the MenuItemsClicked event of the menu. Heres an example from a toolstrip:
ToolbarControl.ItemClicked += new ToolStripItemClickedEventHandler(ToolbarControl_ItemClicked); private void ToolbarControl_ItemClicked(object sender, System.Windows.Forms.ToolStripItemClickedEventArgs e) { //Get the name of the item clicked switch (e.ClickedItem.Name) { case "Item1": DoWork(); break; case "AnotherItem": DoSomethingElse(); break; } }
-
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.To avoid this problem, I would suggest you use a BackgroundWorker. This component will handle much of the threading for you and allow you a way modify controls on the form. It has a few very helpful methods...RunWorkerAsync() , ReportProgress(), and WorkDone(). You can call the ReportProgress method within your for loop like
private void StartProcess() { this.bgWorker.RunWorkerAsync(); } private void bgWorker_DoWork() { AddCNums(); } private void AddCNums() { for(int i = 0; i < 20, i++) { this.bgWorker.ReportProgress(0, i)} } } private void bgWorker_ReportProgress(object sender, ProgressChangedEventArgs e) { if(e.ProgressPercentage == 0) { int i = (int)e.UserState; this.cText.Text += ("Main thread: {0}\n", i); } }
It's very clean. -
Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheetI 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);
-
Floating/Dockable formSweet suite Dockpanel Suite [^]
-
Problem with merging 2 DataTablesWhat did you do next? da1.UpdateCommand = cmdBld.GetUpdateCommand(); ?
-
Problem with merging 2 DataTablesOh ok. I think you just need to create a new command object with an update command. Then set the UpdateCommand of the adapter equal to that command object.
-
Dynamically add TabPages to my form depending on the number of worksheets in a spreadsheetI'm not terribly familiar with the Microsoft.Office namespace but after you get the collection of WorkSheets called sheets, I would think you can loop through them all and create pages. Something like this:
TabPage page; DataGridView dgv; foreach(Worksheet sheet in sheets) { page = new TabPage(); dgv = new DataGridView(); //loop thru records in worksheet. foreach record, add row to datagridview --enter code here-- //after adding records, add datagridview to tab page page.Controls.Add(dgv); //add the page to your TabControl //this line each page to the first position this.tabControl1.TabPages.Insert(0, page); }
Hope this helps. Lester http://www.lestersconyers.com modified on Monday, April 7, 2008 10:25 AMmodified on Monday, April 7, 2008 11:07 AM
-
Problem with merging 2 DataTablesI think you're calling Update() on "db2_table1." Should you be calling it on "db1_table1"?
-
Custom treeview woesHello. I'm working with a custom treeview control for a project and everything works fine. However, I have a little problem. Whenever I add add the treeview to a form and click something else, I can no longer select it with the mouse unless I add a tab page to it. This is getting pretty annoying because I'm actually working with 3 custom treeviews and they all have this same problem. This makes it pretty difficult to be productive. Any suggestions?
-
Load Cursor [modified] Wrong CodeSorry about that. I posted the wrong code sample. I just edited and changed the code. The cursor is not just a black box, though. It's the correct shape as my cursor but its just filled in black. I think it's a problem with the stream converting the byte[] into an image then to a cursor. I cannot get around it.
-
Load Cursor [modified] Wrong CodeHello all. I created an Icon and embedded it in my Resource file. I've been able to get the cursor with the following:
using (MemoryStream resStream = new MemoryStream(SpatialLib.Properties.Resources.MapTipCur, false)) { this.MapControl.Cursor = new Cursor(resStream); }
Now, this successfully loads the cursor but there a huge problem...the cursor is all black. Not the original color. Has anyone gotten around this? Thanks in advance,Lester http://www.lestersconyers.com
modified on Monday, March 10, 2008 4:31 PM
-
Store image in DataTableHello all. I'm trying to store an image in a DataRow but not sure where to start. My DataSet is not tied to a sql server so I don't thing Blob's are what I need. Either way, I don't know where to start. Can anybody help? Thanks in advance
-
One DataGridView, one bindingsource, multiple tablesHello all. I have a datagridview (genericGrid) and a bindingsource (genericBS). genericGrid's datasource is genericBS. I want to programmatically switch genericBS's DataSource and Datamember. Then, display those values on the genericGrid. What I've tried: Using a switch statement to change genericBS's DataSource and DataMember properties on the load. Then, called genericBS.ResetBinding(true). This is supposed to force genericGrid to refresh its rows. Amazingly, it doesn't work. What actually works: I've resorted to creating a bindingsource for each table. Then using a switch statement to change genericGrid.DataSource. This works. But, there has to be a way to use just one bindingsource, right? Any suggestions?