Link to home
Start Free TrialLog in
Avatar of ariez786
ariez786

asked on

CSV to DATATABLE

How data from csv file can be imported to data table in c# (windows application). Also can i make line chart on basis of the data imported from csv? I need code for importing csv as well as making line chart in c# (windows application) .net 2.0. I am also attaching sample of csv file i will be using for this purpose.

Please give me some code .. i have already searched a lot on net but couldn't find any proper soution.
file.txt
ASKER CERTIFIED SOLUTION
Avatar of MogalManic
MogalManic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ariez786
ariez786

ASKER

Thanks a lot Moqal. I also found one code to fill data table but i need to draw graphs by using values from datatable. Where x-axis will cotain time and y axis will have values from 0 -100. And line graphs will be made for all the columns against time. Note that time is also contains in time column.

I am attaching the code i have used and hope you can help me in this problem.

try
            {
               
                DataTable d = new DataTable();
                // Create a new dataset

                d = new System.Data.DataTable("t2e.csv");
                dataGridView1.DataSource = d;
                // Setup connection and query strings
                System.Data.Odbc.OdbcConnectionStringBuilder connection = new System.Data.Odbc.OdbcConnectionStringBuilder();
                connection.Driver = "Microsoft Text Driver (*.txt; *.csv)";
               
                string query = "SELECT * FROM t2e.csv";

                // Use ODBC to connect to the text database
                System.Data.Odbc.OdbcConnection odbcConnection = new System.Data.Odbc.OdbcConnection(connection.ToString());
                System.Data.Odbc.OdbcDataAdapter adapter = new System.Data.Odbc.OdbcDataAdapter(query, odbcConnection);
                adapter.Fill(d);

               
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Error connecting to text file database. {0}", ex.Message), "Accessing Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
               
            }
Hi MogalManic. The code you send me is really great. I tried it and it solved all of my problems. But please help me making line charts from imported data usind the datatable. I am attaching the format of chart i need to make in c#.


linechart.JPG
To build a line chart you need to parse the data a little bit.  Here is the steps I would take:
  1) Compute Min/Max of X-axis values
  2) Compute Min/Max of Y-Axis values
  3) Draw chart axis using above computed min/Max values
  4) Draw lines for X/Y values in dataset

The best bet is to use a existing charting solution instead of trying to re-invent something.  Here are some links I found:
  http://www.codeproject.com/KB/graphics/zedgraph.aspx - Example of an Open Source charting library
  http://www.devcity.net/Articles/229/1/article.aspx - Tutorial which builds a line chart from scratch
Thanks a lot MoqalManic. I also found out this zedgraph library yesterday and i found it very useful. But the code u provided me to read csv file really improved my work. The credit goes to you so i am accepting it as solution.
thanks a lot fro providing me such an excellent code.