I have a listbox popluated from a dataview.
The user can selecte multiple items but I'm having trouble saving each selected item in an Array List.
I have a method to fill the listbox:
DataView dv = new DataView(SqlHelper.ExecuteDataset(cnIPDAdmin, CommandType.StoredProcedure, "gsp_GetCust").Tables);
lbxCust.DataSource = dv;
lbxCust.DisplayMember = "CustomerName";
lbxCust.ValueMember = "CustomerID";
Then I have a method to save all the customers selected in the listbox:
ArrayList alCust = new ArrayList;
int c = lbxCust.SelectedIndices.Count;
for(int i = 1; i <= c; i++)
This code won't even compile, but if I take ".i" off then and I run it and select 3 items, I only capture the first item selected 3 times. Also, it only saves the CustID and not the name.
What do I need to do for the arraylist to capture each individual item selected?
Thanks for your help - I've search several forums and looked through several books and I can't get it to work.
You should look into the <strong>ListBox.SelectedItems</strong> property collection instead. Here is my example with the <strong>CheckedListBox</strong> class but the logic applies.ArrayList eventsToLog = new ArrayList();
foreach(object checkedItem in this.clbEventCodes.CheckedItems)
Great - thanks for the help so far.
Actually both of these examples work...I think, but when I set the arraylist to a listbox.DataSource to view, all that shows is "System.Data.DataRowView" - instead of the actual item?
This is my code now:
foreach(object selectedItem in this.lbxCust.SelectedItems)
lbxTest.DataSource = alCust;
Thank you - you're helping me make progress!
This is how I ended up coding it and it works!!
foreach(DataRowView objDataRowView in lbxCust.SelectedItems)
Thanks for your help icelava, Erymuzuan Mustapa and twisterjosh! I appreciate your input and help.