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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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
ASKER
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.
ASKER
thanks a lot fro providing me such an excellent code.
ASKER
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
dataGridView1.DataSource = d;
// Setup connection and query strings
System.Data.Odbc.OdbcConne
connection.Driver = "Microsoft Text Driver (*.txt; *.csv)";
string query = "SELECT * FROM t2e.csv";
// Use ODBC to connect to the text database
System.Data.Odbc.OdbcConne
System.Data.Odbc.OdbcDataA
adapter.Fill(d);
}
catch (Exception ex)
{
MessageBox.Show(String.For
}