How to Set multiple items as selected in a ListBox with c# [modified]
-
I have a ListBox set as Multiselect. I am populating the table thruough asp.net code. But I am using another dataset to get the items that should be selected. The only item that ends up selected is the last item in the dataset, and I know in several cases there are at least two items that should be selected. The code I am using for setting the selected items is: DataSet ds1 = SqlHelper.ExecuteDataset(ConnStr, "StoredProd", Parm); lbRequestTime.SelectionMode = ListSelectionMode.Multiple; foreach (DataRow row in ds1.Tables[0].Rows) { lbRequestTime.SelectedValue = row["PerfTimeId"].ToString(); } Any help on how to get the Mulitple selected items selected would be great. Thanks, Doug
modified on Tuesday, March 25, 2008 9:33 AM
-
I have a ListBox set as Multiselect. I am populating the table thruough asp.net code. But I am using another dataset to get the items that should be selected. The only item that ends up selected is the last item in the dataset, and I know in several cases there are at least two items that should be selected. The code I am using for setting the selected items is: DataSet ds1 = SqlHelper.ExecuteDataset(ConnStr, "StoredProd", Parm); lbRequestTime.SelectionMode = ListSelectionMode.Multiple; foreach (DataRow row in ds1.Tables[0].Rows) { lbRequestTime.SelectedValue = row["PerfTimeId"].ToString(); } Any help on how to get the Mulitple selected items selected would be great. Thanks, Doug
modified on Tuesday, March 25, 2008 9:33 AM
Hi, you can use the following code
ListItem li; foreach (DataRow row in ds1.Tables[0].Rows) { li = lbRequestTime.Items.FindByValue(row["PerfTimeId"].ToString()); if (li != null) li.Selected = true; }
Do let me know if you need more info. Cheers...Happy Programming, Vimal Raj MCAD.Net www.techisolutions.blogspot.com