Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add data row to datatable at predefined index

I have a datatable with one column:

this.callsTable.Columns.Add("Call", typeof(String));

I then want to add a row to that datatable, but want to give a specific index, the commented number is the desired index:

this.callsTable.Rows.Add("Legs"); //11

Update:

  • Must be able to handle inputting hundreds of rows with unique indexes.
  • The index must be what is defined by me no matter if there are enough rows in the table or not for the insertat function.
like image 997
RSM Avatar asked Dec 17 '13 14:12

RSM


People also ask

How add data row to DataTable?

To add a new row, declare a new variable as type DataRow. A new DataRow object is returned when you call the NewRow method. The DataTable then creates the DataRow object based on the structure of the table, as defined by the DataColumnCollection.

How do you add a row at the top in the DataTable in C#?

Rows is a DataRowCollection which has an InsertAt extension method: DataTable. Rows. InsertAt(DataRow, pos);

What method must you use to add a row to DataTable?

New rows can be added to a DataTable very easily using the row. add()DT API method. Simply call the API function with the data that is to be used for the new row (be it an array or object). Multiple rows can be added using the rows.


1 Answers

You can use DataTable.Rows.InsertAt method.

DataRow dr = callsTable.NewRow(); //Create New Row
dr["Call"] = "Legs";              // Set Column Value
callsTable.Rows.InsertAt(dr, 11); // InsertAt specified position

See: DataRowCollection.InsertAt Method

If the value specified for the pos parameter is greater than the number of rows in the collection, the new row is added to the end.

like image 58
Habib Avatar answered Sep 24 '22 00:09

Habib