How to include a new row in a Dataset

Posted on 2007-10-04
Last Modified: 2013-12-17
I am populating a combo box using a dataset. I manually add one row to the dataset by using this code.
cboMP.DataSource = rsMP.Tables[0];
cboMP.DisplayMember = "marketing_program";
cboMP.ValueMember = "program_id";

I want to add extra 3 rows to the combo box other than the values I get from the dataset. First row should be empty. Second Row should be "Undefined" and 3rd row should be "ALL". I can add first row by

DataRow row = rsMP.Tables[0].NewRow();
rsMP.Tables[0].Rows.InsertAt(row, 0);

but I cannot figure out how to add the other to lines.

Question by:pratikshahse
    LVL 21

    Expert Comment

    Add "All" first, by inserting it at 0
    Then add "Undefined"  by inserting it at 0
    And then Add Blank at last.
    LVL 29

    Accepted Solution

    I believe you have two options only.
    First:  Somehow to add "Undefined" and"All" to the dataset (do you really want to do that?!)
    Second: "Manually" populate the combobox, i.e. not makint the dataset as a DaraSource but rather using a loop and adding value by value. Then you can add any values to you combobox at any positions you'd like.
    LVL 11

    Expert Comment

    by:Mihai Stancescu
    First: you want the 3 new lines to be added first? Or they don't matter their position in the combo box?

    If so you can try this:

    DataTable dt = rsMP.Tables[0].Clone(); // this will create only the columns structure and the table schema, if //any.

    //First empty row....
    DataRow dr = dt.NewRow();
    dr["marketing_program"] = "";
    dr["program_id"] = "-1";
    //The 2nd row
    dr = dt.NewRow();
    dr["marketing_program"] = "Undefined";
    dr["program_id"] = "-2";
    //The 3rd row...
    dr = dt.NewRow();
    dr["marketing_program"] = "ALL";
    dr["program_id"] = "-3";

    //Now to import the remaining rows...
    for (int i = 0; i < rsMP.Tables[0].Rows.Count; i++)
    cboMP.DataSource = dt;
    cboMP.DisplayMember = "marketing_program";
    cboMP.ValueMember = "program_id";

    Hope this helps!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    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…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    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

    8 Experts available now in Live!

    Get 1:1 Help Now