473,473 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Adventures in Excel

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 (header)
{
strConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strFilePath + ";Jet
OLEDB:Engine Type=5;" +
"Extended Properties=\"Excel 8.0;HDR=Yes\"";
}
else
{
strConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strFilePath + ";Jet
OLEDB:Engine Type=5;" +
"Extended Properties=\"Excel 8.0;HDR=No\"";
}
OleDbConnection cnCSV = new
OleDbConnection(strConnectionString);
cnCSV.Open();
OleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM
[Sheet1$]", cnCSV);
OleDbDataAdapter daCSV = new OleDbDataAdapter();
daCSV.SelectCommand = cmdSelect;
dtCSV = new DataTable("Batch");
daCSV.Fill(dtCSV);
cnCSV.Close();
daCSV = null;
return dtCSV;

}

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:

for (int r = 0; r <= dgvMain.Rows.Count - 2; r++)
{
for (int c = 0; c <= dgvMain.Columns.Count; c++)
{
value = dgvMain.Rows[r].Cells[c].Value.ToString();
}
}
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
5 2372
Rahvyn,

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:

private static IEnumerable<DataGridViewColumn>
GetDisplayOrderEnumeration(DataGridViewColumnColle ction columns)
{
// Get the first column.
DataGridViewColumn column =
columns.GetFirstColumn(DataGridViewElementStates.N one);

// Continue while there is a column.
while (column != null)
{
// Yield the column.
yield return column;

// Get the next column.
column = columns.GetNextColumn(column,
DataGridViewElementStates.None, DataGridViewElementStates.None);
}
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rahvyn" <Ra****@discussions.microsoft.comwrote in message
news:D1**********************************@microsof t.com...
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 (header)
{
strConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strFilePath + ";Jet
OLEDB:Engine Type=5;" +
"Extended Properties=\"Excel
8.0;HDR=Yes\"";
}
else
{
strConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strFilePath + ";Jet
OLEDB:Engine Type=5;" +
"Extended Properties=\"Excel
8.0;HDR=No\"";
}
OleDbConnection cnCSV = new
OleDbConnection(strConnectionString);
cnCSV.Open();
OleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM
[Sheet1$]", cnCSV);
OleDbDataAdapter daCSV = new OleDbDataAdapter();
daCSV.SelectCommand = cmdSelect;
dtCSV = new DataTable("Batch");
daCSV.Fill(dtCSV);
cnCSV.Close();
daCSV = null;
return dtCSV;

}

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:

for (int r = 0; r <= dgvMain.Rows.Count - 2; r++)
{
for (int c = 0; c <= dgvMain.Columns.Count; c++)
{
value = dgvMain.Rows[r].Cells[c].Value.ToString();
}
}
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 #2
Thanks Nicholas. I am unfamiliar with the yield keyword. What does this
return?
May 19 '07 #3
Also, how would I use something like this as I loop through the rows /
columns to get the values?

"Rahvyn" wrote:
Thanks Nicholas. I am unfamiliar with the yield keyword. What does this
return?
May 19 '07 #4
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:

for (int r = 0; r <= dgvMain.Rows.Count - 2; r++)
{
foreach (DataGridViewColumn c in
GetDisplayOrderEnumeration(dgvMain.Columns))
{
value = dgvMain.Rows[r].Cells[c.Name].Value.ToString();
}
}

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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rahvyn" <Ra****@discussions.microsoft.comwrote in message
news:86**********************************@microsof t.com...
Also, how would I use something like this as I loop through the rows /
columns to get the values?

"Rahvyn" wrote:
>Thanks Nicholas. I am unfamiliar with the yield keyword. What does this
return?
May 19 '07 #5
Thank you Nicholas, that works perfectly, very elegant solution.

"Nicholas Paldino [.NET/C# MVP]" wrote:
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:

for (int r = 0; r <= dgvMain.Rows.Count - 2; r++)
{
foreach (DataGridViewColumn c in
GetDisplayOrderEnumeration(dgvMain.Columns))
{
value = dgvMain.Rows[r].Cells[c.Name].Value.ToString();
}
}

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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rahvyn" <Ra****@discussions.microsoft.comwrote in message
news:86**********************************@microsof t.com...
Also, how would I use something like this as I loop through the rows /
columns to get the values?

"Rahvyn" wrote:
Thanks Nicholas. I am unfamiliar with the yield keyword. What does this
return?
May 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

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...
1
by: rahvyn | 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.