473,394 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C# Adventures in Excel

2
Hi All;

I'm working on a simple desktop application that does the following:

1 - Reads in an Excel spreadsheet into a DataGridView as follows:
Expand|Select|Wrap|Line Numbers
  1. try
  2.             {
  3.                 string strConnectionString = "";
  4.                 if (header)
  5.                 {
  6.                     strConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
  7.                                  "Data Source=" + strFilePath + ";Jet OLEDB:Engine Type=5;" +
  8.                                  "Extended Properties=\"Excel 8.0;HDR=Yes\"";
  9.                 }
  10.                 else
  11.                 {
  12.                     strConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
  13.                                  "Data Source=" + strFilePath + ";Jet OLEDB:Engine Type=5;" +
  14.                                  "Extended Properties=\"Excel 8.0;HDR=No\"";
  15.                 }
  16.                 OleDbConnection cnCSV = new OleDbConnection(strConnectionString);
  17.                 cnCSV.Open();
  18.                 OleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM [Sheet1$]", cnCSV);
  19.                 OleDbDataAdapter daCSV = new OleDbDataAdapter(); 
  20.                 daCSV.SelectCommand = cmdSelect;
  21.                 dtCSV = new DataTable("Batch");
  22.                 daCSV.Fill(dtCSV);
  23.                 cnCSV.Close();
  24.                 daCSV = null;
  25.                 return dtCSV;
  26.  
  27.             }
  28.  
2 - allows the user to add columns, delete columns, and re-order columns. (I dont update the underlying datatable as if they add a column, it is unbound. But not sure if this is correct)
3 - I then parse through the grid verifying the values in the cells match a specified length, type, etc.
4 - output the grid values to a csv file.

Everything is pretty much working except for parsing the values, and outputing the csv. After the columns are re-ordered, it seems like they retain their original index. So when I parse through like this:

Expand|Select|Wrap|Line Numbers
  1. for (int r = 0; r <= dgvMain.Rows.Count - 2; r++)
  2. {
  3.     for (int c = 0; c <= dgvMain.Columns.Count; c++)
  4.     {
  5.         value = dgvMain.Rows[r].Cells[c].Value.ToString();
  6.     }
  7. }
The variable value contains the value of the original cell. In otherwords, say I have 2 columns, 0 and 1. I then switch their positions, so now have 1, 0. If I parse through as above, I will still be looking at them as 0, 1, not 1, 0. Or, if I move column 10 to position 1, and read that, I wont read the value until c = 10, but I need to read it when c = 1. What I need to do is to reset the indexes of the columns to be in the order that they are displayed. Has anyone done anything like this before?
May 19 '07 #1
1 1306
rahvyn
2
Got a solution if anyone is interested:

In order to do this, you will need to call the GetFirstColumn method on
the Columns collection, as this method will take display order into account
(while the iterator will not). You need to follow that up with a call to
GetNextColumn using the previous column. This makes it perfect for an
iterator:
Expand|Select|Wrap|Line Numbers
  1. private static IEnumerable<DataGridViewColumn>GetDisplayOrderEnumeration(DataGridViewColumnCollection columns)
  2. {
  3.     // Get the first column.
  4.     DataGridViewColumn column = columns.GetFirstColumn(DataGridViewElementStates.None);
  5.  
  6.     // Continue while there is a column.
  7.     while (column != null)
  8.     {
  9.         // Yield the column.
  10.         yield return column;
  11.  
  12.         // Get the next column.
  13.         column = columns.GetNextColumn(column,DataGridViewElementStates.None, DataGridViewElementStates.None);
  14.     }
  15. }
  16.  
The yield keyword will help create an IEnumerable implementation which
you can use a foreach statement to cycle though. You can use it to get the
name of the column to get the value of from the row:
Expand|Select|Wrap|Line Numbers
  1. for (int r = 0; r <= dgvMain.Rows.Count - 2; r++)
  2. {
  3.     foreach (DataGridViewColumn c in GetDisplayOrderEnumeration(dgvMain.Columns))
  4.     {
  5.         value = dgvMain.Rows[r].Cells[c.Name].Value.ToString();
  6.     }
  7. }
  8.  
The method I gave you will give you an enumeration you can cycle through
to get the rows in the display order, which you can then use to access the
values in the same order in the underlying data source.

Thanks to Nicholas over at the msdn forums.
May 19 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
3
by: Otie | last post by:
I found the following under the GetObject help notes and in the example for GetObject: "This example uses the GetObject function to get a reference to a specific Microsoft Excel worksheet...
6
by: Matthew Wieder | last post by:
I have the following requirements: Build a stand-alone C# application that asks the user to click in a cell in an Excel spreadsheet, and then displays the address of that cell in the C#...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
22
by: Howard Kaikow | last post by:
There's a significant problem in automating Excel from VB .NET. Reminds me of a problem I encountered almost 3 years ago that was caused by the Norton Auntie Virus Office plug-in. Can anybody...
9
by: Anthony | last post by:
To me, creating Excel 2003 spreadsheets programmatically via VB.NET hasn't really changed since the days of VB6. That is, I'd do something similar to this Code: Dim ExcelApp As...
4
by: stj911 | last post by:
http://counterpunch.org/rahni04072007.html Test Tube Zealots: The American Chemical Society Terminates the Membership of Chemists from Iran By DAVID N. RAHNI The American Chemical Society...
5
by: =?Utf-8?B?UmFodnlu?= | last post by:
Hi All; I'm working on a simple desktop application that does the following: 1 - Reads in an Excel spreadsheet into a DataGridView as follows: try { string strConnectionString = ""; if...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.