简体   繁体   English

使用C#将CSV文件导入列表

[英]Importing CSV file into a List using C#

I am using C# to import a CSV file into my application 我正在使用C#将CSV文件导入到我的应用程序中

Currently I had a 1 field CSV file. 目前,我有一个1字段CSV文件。 It worked great but now I wanted to add a 3 field CSV file into the same application. 效果很好,但现在我想在同一应用程序中添加一个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. 我认为我应该修改StringValue类,以容纳要从CSV文件导入的多个字段。 I'm not sure how to modify the Value part to return the data I need when I bind it to my dataGridView 我不确定将其绑定到dataGridView时如何修改Value部分以返回所需的数据

Thank you for your input and help/ 感谢您的投入和帮助/

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. 之后,将数据表导入到datagridview中。

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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM