user1693991
user1693991

Reputation: 43

How to add additional elements to a datatable row

dTable.Rows.Add("SSN", "Firstname", "Lastname");

I want to continue adding elements to the same row as the code above with a for loop, how do i do it ?

EDIT:

Thanks for fast answer! sorry i was unclear with my main question How do i continue to expand the row? I have an array with values "Task 1" "Task 2" "Task 3" etc is it possible to make a loop that expands it like this

dTable.Rows.Add("SSN", "Firstname", "Lastname");

dTable.Rows.Add("SSN", "Firstname", "Lastname", arrTask[0]);

dTable.Rows.Add("SSN", "Firstname", "Lastname" arrTask[0], arrTask[1]);

dTable.Rows.Add("SSN", "Firstname", "Lastname" arrTask[0], arrTask[1], arrTask[2]);

Upvotes: 1

Views: 271

Answers (2)

cuongle
cuongle

Reputation: 75306

You can put this code inside loop:

var row = dTable.NewRow();
row.ItemArray = new object[] {"SSN", "Firstname", "Lastname"};

dTable.Rows.Add(row);

Edit: In case you want to add values in existing row:

row.ItemArray = row.ItemArray.Concat(arrTask)
                            .ToArray();

Upvotes: 1

Pushpendra
Pushpendra

Reputation: 810

This code will allow you add the rows

DataRow newRow=dTable.NewRow();

newRow["ColName1"] = "SSN";

newRow["ColName2"] = "Firstname";

newRow["ColName3"] = "Lastname";

dTable.Rows.Add(row);

Upvotes: 0

Related Questions