How to manuever DataTable.rows.add? RRS feed

  • Question

  • Example, I read a lines from CSV file, assuming dataTable.columns.add() have properly been added.   I split it then use the splited data to add a row.  I do this by hard-coded because I know my CSV file contains 5 data (columns).

    myTable.rows.add(splited[0], splited[1], splited[2], splited[3], splited[4]) 

    How do modifed the above code to be effective for an unknown colums?  I was tryting the below but they conflicted with the myTable.rows.add().

    if I loop through the splited, I cannot use the myTable.rows.add().  I was trying to loop through it and concatinate string but I add problem with myTable.rows.add().


    string splited = readLineFromCSVFile().split(',')

    string data;

    for (int i = 0; i < splited.lenght; i++)

          data = data + splited[i];


    I can't do this ->  myTable.rows.add(data);  because data is a string? 

    I am learning. If there is a better way or idea, Please share.
    • Moved by Mathew Aniyan MSFT Sunday, May 22, 2011 10:25 AM Question on Unit Testing framework (From:Visual Studio UI Automation Testing (includes CodedUI))
    Monday, January 31, 2011 9:21 PM

All replies

  • Hi,

    Using foreach loop and iterating over the values you can have datatable with n columns.

    Below is sample

                string teststr = "column1,column2,column3";

                string[] splitted = teststr.Split(',');

                DataTable mytable = new DataTable();

                foreach (string column in splitted)
                    mytable.Columns.Add(column); //this will create columns

                //Now Adding for Adding rows
                splitted = teststr.Split(',');


    • Proposed as answer by Vinay P Tuesday, May 24, 2011 10:25 AM
    Saturday, May 21, 2011 4:39 PM