Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get multiple selected values and items from listbox ?

Status
Not open for further replies.

kgreer

MIS
Jul 18, 2003
155
0
0
US
I have a ListBox that has values. Once a user clicks on a button I need to know what values in the list box is selected. I know this is easy and I am missing something simple. The Listbox is setup to allow the user to select multiple items.

Thanks
 
Here is how I am building the ListBox.....
command.CommandText = "Execute usp_CourseChange_GetSubmitterList";
command.CommandType = CommandType.Text;

conn.Open();
SqlDataReader reader2 = command.ExecuteReader(CommandBehavior.CloseConnection);

string strValue = "";
string strID = "";
string strSetValue = "";

this.lstInstructorID.Items.Clear();
var list = new List<System.Collections.DictionaryEntry>();
while (reader2.Read())
{
strValue = reader2["InstructorName"].ToString();
strID = reader2["InstructorID"].ToString();
list.Add(new System.Collections.DictionaryEntry(reader2["InstructorName"], reader2["InstructorID"]));
}

lstInstructorID.DataSource = null;
lstInstructorID.DataSource = list;
lstInstructorID.DisplayMember = "Key";
lstInstructorID.ValueMember = "Value";
 
To loop selected items in a listbox (try not to use object class)

Code:
List<object> selectedList = new List<object>();
for (int i = 0; i < LstMyListBox.SelectedItems.Count; i++)
{
     selectedList.Add((object)LstMyListBox.SelectedItems[i]); 
}

Does that help?

 
Thanks for the Help....I put the code in and all it is returning is System.Collections.DictionaryEntry

List<object> selectedList = new List<object>();
for (int i = 0; i < lstInstructorID.SelectedItems.Count; i++)
{
label6.Text += "," + (object)lstInstructorID.SelectedItems;
//selectedList.Add((object)LstMyListBox.SelectedItems);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top