Loading a datatable from a CSV file with uneven column numbers per row

Answered Loading a datatable from a CSV file with uneven column numbers per row

  • 2012年4月15日 21:04
     
      包含代码

    Hi,  I am trying to load a datatable from a CSV file with non-standard columns.  I have tried the below code.  I am able to see the data in the row string when I pause the app but it never loads in the dt Datatable...

    Thanks!

        class CSVParser
        {
            public DataTable FullDT(string csvfile)
            {
                string lines = null;
                DataTable dt = new DataTable();
                StreamReader read = new StreamReader(csvfile);
                for (int i = 0; i < 95; i++)
                {
                    dt.Columns.Add();
                }
                while ((lines = read.ReadLine()) != null)
                {
                    string[] row = lines.Split(',');
                    dt.NewRow();
                    dt.Rows.Add(row);
                }
                return dt;
            }
        }
    

全部回复

  • 2012年4月16日 1:02
    版主
     
     已答复 包含代码

    I think what you probably meant to do is this:

    public DataTable FullDT(string csvfile)
    {
        string lines = null;
        DataTable dt = new DataTable();
        StreamReader read = new StreamReader(csvfile);
        for (int i = 0; i < 95; i++)
        {
            dt.Columns.Add();
        }
        DataRow dataRow;
        while ((lines = read.ReadLine()) != null)
        {
            string[] row = lines.Split(',');
            dataRow = dt.NewRow();
            for (int i=0; i < row.Length, i++)
            {
                dataRow[i] = row;
            }
            dt.Rows.Add(dataRow);
        }
        return dt;
    }


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    • 已标记为答案 tlitterio 2012年4月17日 14:41
    •  
  • 2012年4月16日 1:31
     
     

    Thanks for the response, this is driving me nuts!

    I tried it exactly as you typed it and I am getting an ArgumentException was unhandled Error.  Says this row already belongs to this table on the dt.Rows.Add(dataRow); line...  Any ideas?

  • 2012年4月16日 1:34
     
     
    Woops, sorry, I have been starring at this too long; I didn't have it exactly as above...  Now I do and I get an IndexOutOfRangeException was unhandled on the dataRow[i] = row; line
  • 2012年4月16日 1:35
     
     

    Nevermind, I needed to make the count 96 instead of 95.  

    Thanks for the help!

  • 2012年4月16日 4:36
    版主
     
     

    You're welcome ... glad I could help!  =0)

    Don't forget to mark my post as the Answer ...


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

  • 2012年4月16日 15:31
    版主
     
     
    @Bhaskar -- I don't know if @tlitterio wanted/needed column names for his DataTable. There was no specification in his post that the first line of data from his CSV file contained column names. But, if he needs that (or if any lurkers on this thread need that), then your article certainly demonstrates one way to do that ...

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

  • 2012年4月16日 16:58
     
     
  • 2012年4月17日 14:41
     
     
    No, I did not need column headers.  Thanks!
  • 2012年4月17日 14:50
    版主
     
     
    You're welcome! Glad I could help. =0)

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com