I am trying to get the value of the selected item in the listbox using the code below, but it is always returning null string.
DataSet ds = searchforPrice(Co
Elaborating on previous answer by Pir Fahim, he's right but i'm using selectedItem.Text (only way to make it work to me)
Use the SelectedIndexChanged() event to store the data somewhere. In my case, i usually fill a custom class, something like:
class myItem {
string name {get; set;}
string price {get; set;}
string desc {get; set;}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
myItem selected_item = new myItem();
selected_item.name = listBox1.SelectedItem.Text;
Retrieve (selected_item.name);
}
And then you can retrieve the rest of data from a List of "myItems"..
myItem Retrieve (string wanted_item) {
foreach (myItem item in my_items_list) {
if (item.name == wanted_item) {
// This is the selected item
return item;
}
}
return null;
}
To retreive the value of all selected item in à listbox you can cast selected item in DataRowView and then select column where your data is:
foreach(object element in listbox.SelectedItems) {
DataRowView row = (DataRowView)element;
MessageBox.Show(row[0]);
}
If you want to retrieve your value from an list box you should try this:
String itemSelected = numberListBox.GetItemText(numberListBox.SelectedItem);
You can Use This One To get the selected ListItme Name ::
String selectedItem = ((ListBoxItem)ListBox.SelectedItem).Name.ToString();
Make sure that Your each ListBoxItem have a Name property