Unable to retrieve ListBox ValueMember???

  • Thread starter Thread starter Paolo
  • Start date Start date
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
 
ValueMember only makes sense if you are data-binding via DataSource.
Have you done this?

Marc
 
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.
 
Back
Top