Unable to retrieve ListBox ValueMember???

P

Paolo

From a combo box I use the SelectedValue property as a parameter to a query
from which I populate a ListBox


private void cmbxCategory_SelectedValueChanged(object sender, EventArgs e)
{
string theCatId = cmbxCategory.SelectedValue.ToString();

var getSubCat =
from sc in dataSet.SubCategory
where (theCatId == sc.S_CatId)
select sc.S_Description; // get the Description

lsbxSubCat.DataSource = getSubCat.ToList(); // put Description
in ListBox
}

I then want to get the data from the ValueMember of the ListBox's selected
item.
However, using MessageBox to display my required result is showing "".

private void lsbxSubCat_SelectedValueChanged(object sender,
EventArgs e)
{
string theSubCatId = lsbxSubCat.SelectedValue.ToString(); // get
the Id for the Description
MessageBox.Show(theSubCatId);
}

Can anyone point out any obvious errors?

Thanks
 
M

Marc Gravell

ValueMember only makes sense if you are data-binding via DataSource.
Have you done this?

Marc
 
P

Paolo

Marc: yes, I am (on my form_Load event. However my LINQ query was incorrect
as I was only selecting S_Description, I need also to select S_Id, which is
my ValueMember. Everything works as required now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top