|
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
|
|
|
|
|
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") &&
(worksheet.get_Range("O3", "O3").Text.ToString() == "ECS XML Editing Y/N") &&
(worksheet.get_Range("P4", "P4").Text.ToString() == "ECS Client Notification Y/N – If Yes, supply info") &&
(worksheet.get_Range("Q4", "Q4").Text.ToString() == "Notification Type Email SMS or Both") &&
(worksheet.get_Range("R4", "R4").Text.ToString() == "E-Mail Group") &&
(worksheet.get_Range("S4", "S4").Text.ToString() == "SMS Number") &&
(worksheet.get_Range("T4", "T4").Text.ToString() == "Escalation Y/N") &&
(worksheet.get_Range("U4", "U4").Text.ToString() == "Escalate to who?"))
{
//MessageBox.Show("The headings in the spreadsheet are correct! \nYou may proceed.","Headings", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("The Headings should be: \nTemplate, Error Code, Error Name," +
" Error Description, Error Cause, Source, Destination, Category, Severity, Threshold, Responsible for Resolution, Action,ECS Resubmit Y/N, " +
" ECS Auto Resubmit Y/N, ECS XML Editing Y/N, ECS Client Notification Y/N – If Yes, supply info, " +
" Notification Type Email SMS or Both, E-Mail Group, SMS Number, Escalation Y/N, Escalate to who?, " +
". \n\nPlease load the correct file or Click the 'Create Blank Template' button to get a blank Template.", "Error : Headings", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//Check if all the required tables are there
if ((worksheet.get_Range("H3", "H3").Text.ToString() == "Category") &&
(worksheet.get_Range("I3", "I3").Text.ToString() == "Severity"))
{
//MessageBox.Show("All the required tables were found \nYou may proceed.", "Required Tables", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("The following tables are required: \nCategory, Severity\n\nPlease ensure that these tables exist.",
"Error : Required Tables", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Windows.Forms.Application.Exit();
ExcelObj.Visible = false;
}
// loop through the rows of the spreadsheet and place each row in the datagrid
for (int i = 5; i <= endRow; i++)
{
Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A" + i.ToString(), "Z" + i.ToString());
System.Array myvalues = (System.Array)range.Cells.Value2;
string[] strArray = ConvertToStringArray(myvalues);
//Add the rows from the spreadsheet to the datagrid
DataGridViewRow row = new DataGridViewRow();
Object[] vals = strArray;
row.CreateCells(dgOutput, vals);
dgOutput.Rows.Add(row);
}
page.Controls.Add(dgOutput);
tbControl.TabPages.Insert(0, page);
}
}
catch (Exception EX)
{
MessageBox.Show("Error: " + EX.Message, "Error opening Excel..");
}
It seems like I am very close to completing this. Please help.
Mvelo Walaza
Developer
Telkom SA
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
Thank you very much, it worked!!
Mvelo Walaza
Developer
Telkom SA
|
|
|
|
|
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, MessageBoxIcon.Error);
}
Mvelo Walaza
Developer
Telkom SA
|
|
|
|
|
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:
<br />
foreach(TabPage page in tabcontrol.TabPages)<br />
{<br />
DataGridView dgv = (DataGridView)page.Controls[0];<br />
<br />
foreach(DataGridViewRow row in dgv.Rows)<br />
{<br />
<br />
<br />
}<br />
<br />
}<br />
Good luck
Lester
http://www.lestersconyers.com
modified on Wednesday, April 9, 2008 9:40 AM
|
|
|
|
|
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
|
|
|
|
|
I edited the code. Try it.
Lester
http://www.lestersconyers.com
|
|
|
|
|
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
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, MessageBoxIcon.Error);
}
//add worksheet to workbook
_appClass.Workbooks.Add(sheets);
}
}
}
}
Mvelo Walaza
Developer
Telkom SA
|
|
|
|
|
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
|
|
|
|
|
Hiii,
How to write a text in a video files?I want to stamp time in the video files.Plsssssssss help me.
Thanks in advance
Smithakrishnan
|
|
|
|
|
Hello. 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?
Lester
http://www.lestersconyers.com
|
|
|
|
|
Can ne1 tell me how to synchronize my database using gprs???m usin c# .net compact framework.
|
|
|
|
|
Who, on this earth, is "ne1" and why have you directed your question specifically at this person??
(There'a hint buried in this post, if it wasn't obvious)
|
|
|
|
|
Dave Kreskowiak wrote: Who, on this earth,
with the advent of wireless internet, ne1 could be newhere...
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
|
I'm trying merge DataTable "db2_table1" into to DataTable "db1_table1". But it doen't seem to work. Any help will be appreciated....
<br />
public class MergeTableClass<br />
{<br />
private OleDbDataAdapter da1, da2;<br />
private DataSet ds1;<br />
<br />
public void MergeTables(string connS1, string connS2)<br />
{<br />
string connString1 = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + connS1;<br />
string connString2 = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + connS2;<br />
OleDbConnection oleConn1 = new OleDbConnection(connString1);<br />
OleDbConnection oleConn2 = new OleDbConnection(connString2);<br />
string cmd1 = "SELECT *FROM db1_table1";<br />
string cmd2 = "SELECT *FROM db2_table1";<br />
da1 = new OleDbDataAdapter(cmd1, oleConn1);<br />
da2 = new OleDbDataAdapter(cmd2, oleConn2);<br />
ds1 = new DataSet();<br />
<br />
da1.Fill(ds1,"db1_table1");<br />
da2.Fill(ds1,"db2_table1");<br />
<br />
ds1.Tables["db1_table1"].Merge(ds1.Tables["db2_table1"]);<br />
da1.Update(ds1,"db2_table1");<br />
ds1.AcceptChanges();<br />
<br />
}<br />
}<br />
|
|
|
|
|
I think you're calling Update() on "db2_table1." Should you be calling it on "db1_table1"?
Lester
http://www.lestersconyers.com
|
|
|
|
|
I have tried both...still doens't work.
|
|
|
|
|
Oh 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.
Lester
http://www.lestersconyers.com
|
|
|
|
|
I tried to create a OleDbCommandBuilder but it still didn't work...
OleDbCommandBuilder cmdBld = new OleDbCommandBuilder(da1);
|
|
|
|
|
What did you do next?
da1.UpdateCommand = cmdBld.GetUpdateCommand();
?
Lester
http://www.lestersconyers.com
|
|
|
|
|
Nothing yet...
public void MergeTables(string connS1, string connS2)
{
string connString1 = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + connS1;
string connString2 = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + connS2;
OleDbConnection oleConn1 = new OleDbConnection(connString1);
OleDbConnection oleConn2 = new OleDbConnection(connString2);
string cmd1 = "SELECT *FROM db1_table1";
string cmd2 = "SELECT *FROM db2_table1";
da1 = new OleDbDataAdapter(cmd1, oleConn1);
da2 = new OleDbDataAdapter(cmd2, oleConn2);
ds1 = new DataSet();
da1.Fill(ds1, "db1_table1");
da2.Fill(ds1, "db2_table1");
ds1.Tables["db1_table1"].Merge(ds1.Tables["db2_table1"]);
OleDbCommandBuilder cmdBld = new OleDbCommandBuilder(da1);
da1.UpdateCommand = cmdBld.GetUpdateCommand();
@da1.Update(ds1, "db1_table1");
ds1.AcceptChanges();
}
|
|
|
|
|