|
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[0]); 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++) { alCust.Add(lbxCust.SelectedValue.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.
|
| MigrationUser 1 Thursday, May 06, 2004 10:49 AM |
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) { eventsToLog.Add(checkedItem); } |
| MigrationUser 1 Thursday, May 06, 2004 10:05 PM |
you can try this, but it keeps the item in the array list instead not the value
foreach(object o in lbxCust.SelectedItems) { alCust.Add(o); }
|
| MigrationUser 1 Thursday, May 06, 2004 11:04 PM |
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) { alCust.Add(selectedItem); }
lbxTest.DataSource = alCust;
Thank you - you're helping me make progress! |
| MigrationUser 1 Friday, May 07, 2004 10:47 AM |
Close, but no cigar! Trick is, call the ToString method like so:
foreach(object selectedItem in this.lbxCust.SelectedItems) { alCust.Add(selectedItem.ToString()); }
|
| MigrationUser 1 Tuesday, May 11, 2004 12:13 PM |
It didn't change anything to add ".ToString()" - it still shows only System.Data.DataRowView.
I can't figure out what I'm doing wrong - thanks for continuing to help me! |
| MigrationUser 1 Tuesday, May 11, 2004 4:14 PM |
Since you know upon binding your table to the ListBox's DataSource yields DataRowViews, have it cast into <strong>(DataRowView)["PlusYourColumnName"].ToString()</strong> |
| MigrationUser 1 Tuesday, May 11, 2004 8:34 PM |
This is how I ended up coding it and it works!!
foreach(DataRowView objDataRowView in lbxCust.SelectedItems) { alCust.Add(objDataRowView["CustomerName"].ToString()); }
Thanks for your help icelava, Erymuzuan Mustapa and twisterjosh! I appreciate your input and help. |
| MigrationUser 1 Wednesday, May 12, 2004 2:55 PM |