Adding a new row to a datatable in a specific position is a snap. The scenario in this case would be that you have a datatable filled via a stored procedure that is also used in other applications so it can not be altered. The datatable is used to populate a dropdownlist and that list needs to have the always popular ‘Select All’ option as the first item. To add this in you could use code like what is listed below after the datatable is populated from the stored procedure. The example uses two columns in the datatable, ‘DropDownValue’ and ‘ReportItem’ and dt is the already declared DataTable.
VB.Net
Dim allRow as DataRow = dt.NewRow
allRow(“DropDownValue”) = 0
allRow(“ReportItem”) = “Select All”
dt.Rows.InsertAt(allRow, 0)
C#
DataRow allRow = dt.NewRow;
allRow("DropDownValue") = 0;
allRow("ReportItem") = "Select All";
dt.Rows.InsertAt(allRow, 0);
The main part to look at is InsertAt. This allows you to enter in the row you created at the position you need (the first row in the example).
Technorati Tags:
VB.Net,
CSharp