1

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

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.

Once the data is stored into the List, I'm binding it to my 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

Thank you for your input and help/

4

2 回答 2

1

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

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

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";
于 2013-04-22T20:56:41.790 回答
1

Why not juste put the csv into a datatable like so?

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.

于 2013-04-22T16:57:31.373 回答