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:
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.
Rows is a DataRowCollection which has an InsertAt extension method: DataTable. Rows. InsertAt(DataRow, pos);
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With