pratikshahse
asked on
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.Insert At(row, 0);
but I cannot figure out how to add the other to lines.
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.Insert
but I cannot figure out how to add the other to lines.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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";
dt.Rows.Add(dr);
//The 2nd row
dr = dt.NewRow();
dr["marketing_program"] = "Undefined";
dr["program_id"] = "-2";
dt.Rows.Add(dr);
//The 3rd row...
dr = dt.NewRow();
dr["marketing_program"] = "ALL";
dr["program_id"] = "-3";
dt.Rows.Add(dr);
dt.AcceptChanges();
//Now to import the remaining rows...
for (int i = 0; i < rsMP.Tables[0].Rows.Count; i++)
{
dt.ImportRow(rsMP.Tables[0 ].Rows[i]) ;
}
cboMP.DataSource = dt;
cboMP.DisplayMember = "marketing_program";
cboMP.ValueMember = "program_id";
Hope this helps!
Regards,
Mishu
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";
dt.Rows.Add(dr);
//The 2nd row
dr = dt.NewRow();
dr["marketing_program"] = "Undefined";
dr["program_id"] = "-2";
dt.Rows.Add(dr);
//The 3rd row...
dr = dt.NewRow();
dr["marketing_program"] = "ALL";
dr["program_id"] = "-3";
dt.Rows.Add(dr);
dt.AcceptChanges();
//Now to import the remaining rows...
for (int i = 0; i < rsMP.Tables[0].Rows.Count;
{
dt.ImportRow(rsMP.Tables[0
}
cboMP.DataSource = dt;
cboMP.DisplayMember = "marketing_program";
cboMP.ValueMember = "program_id";
Hope this helps!
Regards,
Mishu
Then add "Undefined" by inserting it at 0
And then Add Blank at last.