Question

I have a .csv file that has duplicate column names, namely the word "Type" is repeated multiple times.

I have a basic .csv reader that reads into an array then splits on the ','. However because it then gets assigned to a DataTable I get issues with any .csv file where I have duplicate column names.

Whats the best way to get around this?

    public void parseCSV(string file)
    {
        string[] Lines = File.ReadAllLines(file);

        string[] Fields;
        Fields = Lines[0].Split(new char[] { ',' });

        int Cols = Fields.GetLength(0);
        DataTable dt = new DataTable();

        //1st row must be column names; force lower case to ensure matching later on.
        for (int i = 0; i < Cols; i++)
            dt.Columns.Add(Fields[i], typeof(string));

        DataRow Row;
        for (int i = 1; i < Lines.GetLength(0); i++)
        {
            Fields = Lines[i].Split(new char[] { ',' });
            Row = dt.NewRow();
            for (int f = 0; f < Cols; f++)
                Row[f] = Fields[f];
            dt.Rows.Add(Row);

            dataGridView.DataSource = dt;
        }
    }

Edit: Test .csv file I am using that throws error about col2

col1,col2,col2,col3,col4,Time Modified
1,1,123456,1,1,15:30
2,2,654321,2,2,15:31
Was it helpful?

Solution

You could add a suffix like Type_1:

for (int i = 0; i < Cols; i++)
{
    string columnName = Fields[i];
    int num = 0;
    while (dt.Columns.Contains(columnName))
        columnName = string.Format("{0}_{1}", Fields[i], ++num);
    dt.Columns.Add(columnName);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top