• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Assigning headers to a datatable

I am reading a table of data into excel using ado.net.  Unfortunately my data and the headers for that data are separated on the excel sheet(the headers are actually several hundred rows below the start of the data)  I use the below code to import the table into a datatable and it works wonderfully.  How would I get the header data from the "header" table to become the header for the "data" table?
myDataSet = new DataSet();
            filename = "";
 
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = ""; // Default file name
            dlg.DefaultExt = ".xls"; // Default file extension
            dlg.Filter = "Excel documents (.xls)|*.xls"; // Filter files by extension
 
            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();
 
            string file;
            if (result == true)
            {
                // Open document
                file = dlg.FileName;
                int filedel = file.LastIndexOf("\\");
                filename = file.Remove(0, filedel+1);
            }
            //else end function
            else
            {
                return;
            }
 
 
            string connectString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties= ";
        connectString = connectString+ @"""Excel 8.0;HDR=YES;""";
 
 
 
            OleDbConnection connection =
                    new OleDbConnection(connectString);
 
            string query = "SELECT * FROM [Lap Sim$B24:FG724] "; //where (NOT ([Bank] IS NULL))
            string queryheader = "SELECT * FROM [Lap Sim$B751:FG751] "; //where (NOT ([Bank] IS NULL))
            
            
            OleDbConnection myAccessConn = null;
            try
            {
                myAccessConn = new OleDbConnection(connectString);
            }
            catch(Exception ex)
            {
                  Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
                  return;
            }
 
            try
            {
 
                OleDbCommand myAccessCommand = new OleDbCommand(query, myAccessConn);
                  OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
 
                  myAccessConn.Open();
                  myDataAdapter.Fill(myDataSet,"data");
 
 
 
                  myAccessCommand = new OleDbCommand(queryheader, myAccessConn);
                  myDataAdapter = new OleDbDataAdapter(myAccessCommand);
 
                  //myAccessConn.Open();
                  myDataAdapter.Fill(myDataSet, "header");
 
            }
            catch (Exception ex)
            {
                  Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
                  return;
            }
            finally
            {
                myAccessConn.Close();
                connection.Close();
            }
 
  
            cmbXAxis.Items.Clear();
            cmbYAxis.Items.Clear();
            foreach (DataColumn column in myDataSet.Tables["data"].Columns)
            {
                string colname = column.ColumnName.ToString();
                cmbXAxis.Items.Add(colname);
                cmbYAxis.Items.Add(colname);
            }
            
 
        }

Open in new window

0
perrytaylor
Asked:
perrytaylor
  • 2
1 Solution
 
RimvisCommented:
Like this?
...
            finally
            {
                myAccessConn.Close();
                connection.Close();
            }
            
            //Rename columns
            for (int i = 0; i < myDataSet.Tables["header"].Columns.Count; i++)
            {
                DataColumn dc = myDataSet.Tables["data"].Columns[i]; 
                dc.Caption = dc.ColumnName = myDataSet.Tables["header"].Columns[i].ColumnName;
            }
...

Open in new window

0
 
perrytaylorAuthor Commented:
This is actually what I came up with that seems to work,  what is the dc.caption all about?
thanks

string name;
            for(int b = 0; b
0
 
RimvisCommented:
"dc.Caption = dc.ColumnName =" sets same value for both Caption and ColumnName properties. I was not sure which property you are referring as "header "
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now