Add data row to datatable at predefined index

♀尐吖头ヾ 提交于 2019-12-10 05:24:47

问题


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.

回答1:


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.



来源:https://stackoverflow.com/questions/20636398/add-data-row-to-datatable-at-predefined-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!