• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

how to add a new row to datatable as first row

I have a dropdown list which binds to a dataset. I'd like to see the dropdown list to show a blank line when it is loaded first time.  Here is what I did:

oDS.tables(0).rows.add("", "")

However, the blank line went to the bottom of the dropdown list.  How can I add the blank line to the top?  Thanks!
2 Solutions
It depends on when you add the row to your dataset.  If you fill the dataset, and then add the row, your row will be at the bottom.  However, if your dataset is already set up you could add the row, and then fill the dataset with your data like this:

oDS.tables(0).rows.add("", "")
<Fill Dataset Code>
<Bind Dataset to DDL Code>
Try this:

DataRow dr = oDS.Tables(0).NewRow()
oDS.Tables(0).Rows.InsertAt(dr, 0)
Don't think you need to add it to the data set, you can add it to the dropdownlist.

Code like this:

dropdownlist1.datasource = ds
dropdownlist1.insert(0, "")
oh, yeah, if you're just trying to get a new item in the DropDownList don't add it to the DataTable.

DropDownList1.Items.Insert(0, new ListItem("No Selection", "0"));
tz478Author Commented:
Both ways worked.  Thanks!  I'll add additional 100 points for laotzi2000.

The correct code for dropdown should be: dropdownlist1.items.insert(...).

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now