Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 19th, 2007, 11:33 AM
Registered User
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# 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.
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?


 
Old May 19th, 2007, 02:22 PM
Registered User
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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:
Code:
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);
    }
}
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:
Code:
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.

Thanks to Nicholas over at the msdn forums.

 
Old May 22nd, 2007, 12:21 PM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for posting your solution!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in opening excel file in MS Excel 2000 kallol Visual C++ 0 November 16th, 2007 05:48 AM
ASP to Excel - excel output formatting issue mat41 Classic ASP Professional 0 August 13th, 2006 06:41 AM
write multi-sheet Excel w/o Excel.Application manmoth Classic ASP Components 2 November 22nd, 2005 10:56 AM
Excel in win 2000 to excel winxp (excel 2002) Max Excel VBA 3 August 28th, 2003 04:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.