Bunch's Blog

One day I'll have a catchy subtitle, one day
posts - 77, comments - 89, trackbacks - 0

My Links

News

Tag Cloud

Archives

Green

Inserting a New Row into a Datatable

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: ,
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Friday, October 30, 2009 12:36 AM |

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: