• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • 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!


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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