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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

binding a listbox

Status
Not open for further replies.

luisve

Programmer
Jun 24, 2005
29
US
hi i have a listbox that is bind with a dataset, this part works fine all the records on the dataset are on the list at run time, my problem is when triying to use the values from the listbox every item that i get from this list get the value:

system.data........

How can i use the value that is display in the list box??

thanks
 
lstListBox.SelectedItem.Text;

I think.

You might want to use a ListView.

then it would be:

lstListView.SelectedItems[0].Text; //Gives you the text of the first selected item.
 
lstListBox.SelectedValue would give you the value from the list box.

I'm not sure how you are binding the listbox but this is how I do it with a dataset. In this case I'm binding two fields for the listbox, one to contain the value and the other for the display.

Code:
lstListBox.DataSource = ds.Tables[0];
lstListBox.ValueMember = ds.Tables[0].Columns[0].ToString();
lstListBox.DisplayMember = ds.Tables[0].Columns[1].ToString();

Then I use the SelectedIndex_Changed event to derive the value of the list box
Code:
private void lstListBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			
			try
			{
				
				int listValue =Convert.ToInt32(this.lstListBox.SelectedValue));
					
				
				
			}
			catch(Exception ex)
			{
				string msg = "Error Message: " + ex.Message + " " + ex.GetType().ToString();				
				MessageBox.Show(msg);
			}
			finally
			{
			}
		}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top