|
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();
}
|
|
|
|
|
Found it..
The problem was that after the Merge none of the rows in data table had the state RowState Changed or Added. The update methon had nothing to apply.
This peace of code made the whole class work as it should:
foreach (DataRow row in ds1.Tables["db2_table1"].Rows)<br />
row.SetAdded();
Thanks
|
|
|
|
|
hi
I am using a combobox to load items using datatable. i am using sqlserver2005.when i tried to load a count of 32500 rows in a combobox , my application hangs more than for 5 min.
Any body give me solution.
Below is my code:
<br />
private void _popItemCombo()
{<br />
clsGrpObj = new clsGroupManip();<br />
clsDataAccObj = new clsGloDataAcces();<br />
dtItem = new DataTable();
dtItem = clsGrpObj._selItem_Assembly();<br />
for (int i = 0; i < dtItem.Rows.Count - 1; i++)<br />
{<br />
string item = dtItem.Rows[i]["ItemCode"].ToString();<br />
cmbItemCode.Items.Add(item);
}<br />
<br />
}<br />
help me,
Thank u
kssk
|
|
|
|
|
You should use cmbItemCode.DateSource = dtItem and set the DisplayMember and ValueMember instead.
Any auto search on the combobox? I would have to scroll 30000 rows on 8 items drop down to select a customer....
|
|
|
|
|
hi darkelv
just i tried that , it also takes exactly 6 seconds. If i ignore that
i am experiencing the same trouble in DataGridview combobox column. Can u tell me how to overcome that .
Thank u for ur quick response
kssk
senthil
|
|
|
|
|
Loading 30,000 items into a combo box is plainly not the right thing to do. A combo box is used to select from, with this many items it will be, to all intents, unusable.
You need to rethink what you are trying to achieve. Can you filter your list in some way?
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Like Ashfield said, there's no way around it. You just have to load less data. The problem will be worse if you do it in the DataGridViewComboboxColumn.
You can subclass the combobox, let use enter the beginning letter of the item, and handle the text change or something. Once the control detected that use entered something, populate the combobox with the data beginning with the letter user entered, etc.
Or use a picker dialog.
|
|
|
|
|
I have a table and I want to store the whole database into a string array..
How do I do it?
Say I have a table with 10 fields and suppose the table contains 1000 entries.
I want to store each value of a field delimited by a character '|' and so there should be 1000 entries like this in the string array..
Som
|
|
|
|
|
make a loop that will read all rows and write in a string variable, you can also use string.Join() for this purpose
eg.
string line="";
for(int a = 0;a< DT.Rows.Count;a++)
{
string[] row = DT.Rows[a].ItemsArray;
line += string.Join("|", row) +"\n";
}
code is not tested, hope it will help
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87??6?N8?BcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN%
Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y<p?jxsg-b$f4ia>
--------------------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
What is DT here..
Cant we use a SQL query to query the whole table from the database and then build up the string array?
If this is possible then please advice on this logic..
Som
|
|
|
|
|
DT is DataTable, assign a SQLDataReader to DataTable, it will read all the data from sql table or use DataSet
for more info use google
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87??6?N8?BcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN%
Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y<p?jxsg-b$f4ia>
--------------------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|