使用C#导入CSV文件导入列表 [英] Importing CSV file into a List using C#

查看:333
本文介绍了使用C#导入CSV文件导入列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#导入CSV文件导入我的应用程序

I am using C# to import a CSV file into my application

目前我有一个1场CSV文件。它的工作很好,但现在我想补充一个3场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.

一旦数据被存储到列表中,我将它绑定到我的DataGridView的

Once the data is stored into the List, I'm binding it to my DataGridView

下面是培训相关代码,我已经写了。如果您看到是不是我的问题的一部分,但可能是一个问题的任何问题(S),请随时喊出来。 。我总是在寻找学习和提高我的代码

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



我想我应该修改我的StringValue类来保存多个字段,我'米我的CSV文件导入。我不知道如何修改值部分,当我把它绑定到我的dataGridView回到我所需要的数据。

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

感谢您的投入和帮助/

Thank you for your input and help/

推荐答案

在你的实体(的StringValue),只要你想,你可以添加任意多的属性,包含尽可能多的值作为您想。

In your entity (StringValue) you can add as many properties as you want, containing as many values as your want.

您可以通过设置列DataPropertyName与你绑定到该属性的名称你的dataGridView的每一列绑定。

You can bind each column of your dataGridView by setting the columns DataPropertyName with the name of the property you are binding to.

例如,你的实体有两个属性:

For example, your entity has two properties:

class MyValues
{
    public string FirstName {get;set;}
    public string LastName {get;set;}
}

你添加的这个集合yuur数据收集,您绑定到您的网格

You add a collection of this to yuur data-collection, which you bind to your grid.

您的网格可以配置路由为:

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

这篇关于使用C#导入CSV文件导入列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆