How to add a column and values in to existing Data column from a string array

Viewed 241

I have to add a data column in to the existing data table.

I need to add one more column and data to that table from a .txt file in to 0th column and from second column I need to keep the DB data.

I am not using any grid view and stream reader to my app. I need to use File.ReadAllLines();

My .txt file is like:

ClaimID
val1      
val3      
val5

I tried something and the data table is not coming properly. Data table coming like showing in the picture below (data is starting from last row of first column and skipped first rows):

enter image description here

What I have tried is:

if (tableName == "TestData")    
{ 
    var pathC = @"H:\claimdetails\claims\Claims.txt";

    string[] result = File.ReadAllLines(pathC);

    DataColumn Col = table.Columns.Add("Claim_ID", typeof(String));

    Col.SetOrdinal(0); // set column to first position

    DataRow row;

    for (int i = 0; i < result.Length; i++)
    {
        row = table.NewRow();

        row["Claim_ID"] = result[i];

        table.Rows.Add(row);
    }
}

adapter.Fill(table);

How can I get a proper data table?

1 Answers

Instead of adding rows to the data table, you can try, table.Rows[i].[0] = result[i]

Related