locked
Problem with data from a CSV file RRS feed

  • Question

  • Hi everyone,

     

    I hope that someone can help me with this one.

    I am writing a Windows Forms application using C# 2005.

    I have been attempting to populate a DataGridView object with data from a CSV file.

     

    My CSV data looks like:

    Stream No.,Die No,Date,Time,Thickness,Status,Track
    Stream 1,8500000/A5,Sep. 03 2007,10:56:25,2.3895,OK,0.4743
    Stream 1,8500000/A5,Sep. 03 2007,10:56:31,2.3919,OK,0.4743
    Stream 1,8500000/A5,Sep. 03 2007,10:56:37,2.3944,OK,0.4743
    Stream 1,8500000/A5,Sep. 03 2007,10:56:43,2.3913,OK,0.4743

     

    I don't need the 'Stream No' or  'Die No' data, so

     

    My code is:

    string tempPath = "C:\Data";

    string strConn = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + tempPath + @"\;Extensions=asc,csv,tab,txt";

    OdbcConnection conn = new OdbcConnection(strConn);

    OdbcDataAdapter da = new OdbcDataAdapter("Select Date,Time,Thickness,Status,Track from temp.csv", conn);

    DataTable dt = new DataTable();

    da.Fill(dt);

    dgvData.DataSource = dt;

     

    This works fine with one exception.

     

    The data in the Time column is incorrect as below:

     

    Date                Time                     Thickness     Status    Track

    Sep. 03 2007    30/12/1899 10:56   2.3895          OK         0.4743
    Sep. 03 2007    30/12/1899 10:56   2.3919          OK         0.4743
    Sep. 03 2007    30/12/1899 10:56   2.3944          OK         0.4743
    Sep. 03 2007    30/12/1899 10:56   2.3913          OK         0.4743

     

    The correct time has been truncated and I have no idea where the 30/12/1899 comes from.

     

    Can anyone shed any light on this for me?

     

    Many thanks

     

    John Woodiwiss

    Live long, prosper and understand C#?

     

    Monday, January 28, 2008 10:52 AM

Answers

  • Hi John!

     

    The reason is, you display the time as a DateTime obj, but in the specific field from the CSV-file, you don't set the date-, only the Time parameter.

     

    If you were to manually fill the dgv, you would do something like this

     

    Code Snippet
    dtTime1.ToLongTimeString()

     

     

    with dtTime1 as being your Time read from the CSV file.

     

    but in you case, take a look at this site, it is addressing the same issue, and bringing the solution.

     

    http://dotnetjunkies.com/Article/F25D02EA-7763-4C96-AA27-260388F848D9.dcik

     

    /Lars

    Monday, January 28, 2008 11:38 AM

All replies

  • Hi John!

     

    The reason is, you display the time as a DateTime obj, but in the specific field from the CSV-file, you don't set the date-, only the Time parameter.

     

    If you were to manually fill the dgv, you would do something like this

     

    Code Snippet
    dtTime1.ToLongTimeString()

     

     

    with dtTime1 as being your Time read from the CSV file.

     

    but in you case, take a look at this site, it is addressing the same issue, and bringing the solution.

     

    http://dotnetjunkies.com/Article/F25D02EA-7763-4C96-AA27-260388F848D9.dcik

     

    /Lars

    Monday, January 28, 2008 11:38 AM
  • Hi Lars,

     

    Many thanks for your quick response to my query.

     

    The link was aimed more at DataView, but it pointed me in the right direction.

     

    Changing my code to:

     

    string tempPath = @"C:\Data";

    string strConn = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + tempPath + @"\;Extensions=asc,csv,tab,txt";

    OdbcConnection conn = new OdbcConnection(strConn);

    OdbcDataAdapter da = new OdbcDataAdapter("Select Date,Time,Thickness,Status,Track from temp.csv", conn);

    DataTable dt = new DataTable();

    da.Fill(dt);

    dgvData.DataSource = dt;

    dgvData.Columns[1].DefaultCellStyle.Format = "T";

     

    has fixed things. 

     

    I had assumed that the DGV would interpret incoming data from the CSV file as all strings. 

     

    Once again, many thanks for your help.

     

    Regards

     

    John Woodiwiss

    Live long, prosper and understand C#?

    Monday, January 28, 2008 2:11 PM