不同列的数据表

时间:2014-10-31 07:38:45

标签: c# arrays csv datatable

我需要读取csv数据并将每个列(我只有两个)复制到两个不同的数据表中并将它们转换为数组。所以最后我需要两个数组,以便在array1中有column1行,在array2中有column2行。怎么做?我甚至需要数据表吗?

编辑:我尝试了这个,但得到了一个" OutofBound"例外

DataTable csvData = new DataTable();
csvData.Columns.Add("Date", typeof(string));
csvData.Columns.Add("Value", typeof(double));

string dir = @"C:\Main\test.csv"; 

DataRow dr = csvData.NewRow();
using (StreamReader sr = new StreamReader(dir))
{
    string line = string.Empty;
    while ((line = sr.ReadLine()) != null)
    {
        string[] strRow = line.Split(',');

        dr["Date"] = strRow[0];
        dr["Value"] = strRow[1];

        csvData.Rows.Add(dr);
    }
}

我尝试了不同的东西(应该有效):

var date = new List<String>();
var value = new List<String>();

string dir = @"C:\Main\test.csv";

using (var reader = new System.IO.StreamReader(dir))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
       var fields = line.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
       date.Add(fields[0]);
       if (fields.Length > 1)
           value.Add(fields[1]);
    }
}
string[] _date = date.ToArray();
string[] _value = value.ToArray();

现在我想将数组绑定到图表但是当我尝试这个时:

chart1.Series["Forecast"].Points.DataBindXY("_date","_value");
chart1.Series["Forecast"].ChartType = SeriesChartType.Spline;

我收到System.Argument.Exception 它告诉我Y-Values只能绑定到字符串对象

1 个答案:

答案 0 :(得分:0)

你真的不需要DataTable。你可以尝试这样的事情:

// read all lines from file
string[] lines = File.ReadAllLines(fileName);

// Create lists for first and second column
List<string> cols1 = new List<string>();
List<string> cols2 = new List<string>();

// Fill lists. This throws an exception if there's a line with only one column
foreach (string line in lines)
{
    string[] parts = line.Split(',');

    // Make sure that each line really contains at least two columns!
    if (parts.Length < 2)
        throw new ArgumentException(String.Format("The following line contains only one column: '{0}'", line));

    cols1.Add(parts[0]);
    cold2.Add(parts[1]);
}

// Create arrays from lists
string[] colArray1 = cols1.ToArray();
string[] colArray2 = cols2.ToArray();

当然,所有这些都可以使用LINQ在更少的行中完成,但这段代码更容易理解。


编辑:您发布的代码无法在文件中每行创建一行。它只在DataTable中创建一行,并为文件中的每一行修改它。您需要使用我的代码或将该行放在循环中,如下所示:

DataTable csvData = new DataTable();
csvData.Columns.Add("Date", typeof(string));
csvData.Columns.Add("Value", typeof(double));

string dir = @"C:\Main\test.csv"; 

using (StreamReader sr = new StreamReader(dir))
{
      string line = string.Empty;
      while ((line = sr.ReadLine()) != null)
      {
         DataRow dr = csvData.NewRow();
         string[] strRow = line.Split(',');

         dr["Date"] = strRow[0];
         dr["Value"] = strRow[1];

         csvData.Rows.Add(dr);
      }
}

编辑2:你得到的例外可能暗示至少有一行不包含两列。您可以通过修改代码轻松地检查这一点:

string[] strRow = line.Split(',');
if (strRow.Length < 2)
    throw new ArgumentException(String.Format("The following line contains only one column: '{0}'", line));

现在你应该得到一个不同的例外,说明哪一行会导致错误。