how to add a new row to datatable as first row

Posted on 2005-05-10
Last Modified: 2010-08-05
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!
Question by:tz478
    LVL 13

    Expert Comment

    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>
    LVL 9

    Accepted Solution

    Try this:

    DataRow dr = oDS.Tables(0).NewRow()
    oDS.Tables(0).Rows.InsertAt(dr, 0)
    LVL 12

    Assisted Solution

    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, "")
    LVL 9

    Expert Comment

    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"));
    LVL 1

    Author Comment

    Both ways worked.  Thanks!  I'll add additional 100 points for laotzi2000.

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now