使用 C# 将 CSV 文件导入列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16152453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Importing CSV file into a List using C#
提问by Cocoa Dev
I am using C# to import a CSV file into my application
我正在使用 C# 将 CSV 文件导入我的应用程序
Currently I had a 1 field CSV file. It worked great but now I wanted to add a 3 field CSV file into the same application.
目前我有一个 1 字段的 CSV 文件。它工作得很好,但现在我想将一个 3 字段的 CSV 文件添加到同一个应用程序中。
Once the data is stored into the List, I'm binding it to my DataGridView
一旦数据存储到列表中,我将它绑定到我的 DataGridView
Here is the relevent code I've written. If you see any issue(s) that aren't part of my problem but can be a problem, please feel free to shout them out. Im always looking to learn and improve my code.
这是我编写的相关代码。如果您看到任何不属于我的问题但可能是问题的问题,请随时大声喊出来。我一直在寻找学习和改进我的代码。
BindingList<StringValue> data = new BindingList<StringValue>();
private void importExcelFile()
{
TextFieldParser parser = new TextFieldParser(fileName);
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
while (!parser.EndOfData)
{
//Processing row
string[] fields = parser.ReadFields();
foreach (string field in fields)
{
StringValue s = new StringValue(field);
// Issue is here. It adds it to a single dimension array. What can I do to make it multi-dimension?
data.Add(s);
}
}
parser.Close();
}
private void OnBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
importExcelFile();
}
private void OnBackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
dataGridView1.DataSource = data;
dataGridView1.Columns[1].Name = "URL";
dataGridView1.Columns[1].HeaderText = "URL";
dataGridView1.Columns[1].Width = 300;
dataGridView1.Columns[1].ReadOnly = true;
dataGridView1.AutoResizeColumns();
toolStripStatusLabel1.Text = dataGridView1.RowCount.ToString() + " Number Of Websites";
}
class StringValue
{
string day, time, url;
public StringValue(string s)
{
_value = s;
}
public StringValue(string[] s)
{
day = s[0];
time = s[1];
url = s[2];
}
public string Value { get { return _value; } set { _value = value; } }
string _value;
}
I think I should modify my StringValue class to hold the multiple fields that I'm importing from my CSV file. I'm not sure how to modify the Value part to return the data I need when I bind it to my dataGridView
我想我应该修改我的 StringValue 类来保存我从我的 CSV 文件导入的多个字段。当我将它绑定到我的 dataGridView 时,我不确定如何修改 Value 部分以返回我需要的数据
Thank you for your input and help/
感谢您的意见和帮助/
回答by Sebastien
Why not juste put the csv into a datatable like so?
为什么不把 csv 放到像这样的数据表中?
private DataTable GetDataTableFromCsv(string path)
{
DataTable dataTable = new DataTable();
String[] csv = File.ReadAllLines(path);
foreach (string csvrow in csv)
{
var fields = csvrow.Split(','); // csv delimiter
var row = dataTable.NewRow();
row.ItemArray = fields;
dataTable.Rows.Add(row);
}
return dataTable;
}
after that juste import the datatable into your datagridview.
之后,将数据表导入到您的数据网格视图中。
回答by Martin Mulder
In your entity (StringValue) you can add as many properties as you want, containing as many values as your want.
在您的实体 (StringValue) 中,您可以根据需要添加任意数量的属性,包含任意数量的值。
You can bind each column of your dataGridView by setting the columns DataPropertyName with the name of the property you are binding to.
您可以通过将列 DataPropertyName 设置为要绑定到的属性的名称来绑定 dataGridView 的每一列。
For example, your entity has two properties:
例如,您的实体有两个属性:
class MyValues
{
public string FirstName {get;set;}
public string LastName {get;set;}
}
You add a collection of this to yuur data-collection, which you bind to your grid.
您将这个集合添加到 yuur 数据集合,您将其绑定到您的网格。
Your grid can be configures as:
您的网格可以配置为:
dataGridView1.Columns[0].Name = "FirstName";
dataGridView1.Columns[0].HeaderText = "FirstName";
dataGridView1.Columns[0].DataPropertyName = "FirstName";
dataGridView1.Columns[1].Name = "LastName";
dataGridView1.Columns[1].HeaderText = "LastName";
dataGridView1.Columns[1].DataPropertyName = "LastName";

