PC Review


Reply
Thread Tools Rate Thread

Adventures in Excel

 
 
=?Utf-8?B?UmFodnlu?=
Guest
Posts: n/a
 
      19th May 2007
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?

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      19th May 2007
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(DataGridViewColumnCollection columns)
{
// Get the first column.
DataGridViewColumn column =
columns.GetFirstColumn(DataGridViewElementStates.None);

// 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]
- (E-Mail Removed)

"Rahvyn" <(E-Mail Removed)> wrote in message
news1997052-F82C-4C7B-9C15-(E-Mail Removed)...
> 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?
>


 
Reply With Quote
 
=?Utf-8?B?UmFodnlu?=
Guest
Posts: n/a
 
      19th May 2007
Thanks Nicholas. I am unfamiliar with the yield keyword. What does this
return?
 
Reply With Quote
 
=?Utf-8?B?UmFodnlu?=
Guest
Posts: n/a
 
      19th May 2007
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?

 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      19th May 2007
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]
- (E-Mail Removed)

"Rahvyn" <(E-Mail Removed)> wrote in message
news:86579610-98A7-4F5B-8A77-(E-Mail Removed)...
> 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?


 
Reply With Quote
 
=?Utf-8?B?UmFodnlu?=
Guest
Posts: n/a
 
      19th May 2007
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]
> - (E-Mail Removed)
>
> "Rahvyn" <(E-Mail Removed)> wrote in message
> news:86579610-98A7-4F5B-8A77-(E-Mail Removed)...
> > 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?

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adventures with McAfee Nurktwn Anti-Virus 8 12th Jul 2006 11:48 AM
More adventures in learning OOP RSH Microsoft C# .NET 14 16th Dec 2005 03:14 PM
Oracle Adventures Chris Botha Microsoft ADO .NET 13 17th Nov 2005 06:15 PM
Oracle Adventures Chris Botha Microsoft ASP .NET 13 17th Nov 2005 06:15 PM
The Further Adventures of DFS over DSL Gordon Fecyk Microsoft Windows 2000 Active Directory 0 23rd Aug 2004 05:09 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:53 AM.