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

How to include a new row in a Dataset

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.

1 Solution
Add "All" first, by inserting it at 0
Then add "Undefined"  by inserting it at 0
And then Add Blank at last.
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.
Mihai StancescuSoftware Engineer Commented:
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!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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