Unable to retrieve ListBox ValueMember???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?UGFvbG8=?=

    Unable to retrieve ListBox ValueMember???

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


    private void cmbxCategory_Se lectedValueChan ged(object sender, EventArgs e)
    {
    string theCatId = cmbxCategory.Se lectedValue.ToS tring();

    var getSubCat =
    from sc in dataSet.SubCate gory
    where (theCatId == sc.S_CatId)
    select sc.S_Descriptio n; // get the Description

    lsbxSubCat.Data Source = getSubCat.ToLis t(); // 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_Sele ctedValueChange d(object sender,
    EventArgs e)
    {
    string theSubCatId = lsbxSubCat.Sele ctedValue.ToStr ing(); // get
    the Id for the Description
    MessageBox.Show (theSubCatId);
    }

    Can anyone point out any obvious errors?

    Thanks
  • Marc Gravell

    #2
    Re: Unable to retrieve ListBox ValueMember???

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

    Marc

    Comment

    • =?Utf-8?B?UGFvbG8=?=

      #3
      Re: Unable to retrieve ListBox ValueMember???

      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.

      "Marc Gravell" wrote:
      ValueMember only makes sense if you are data-binding via DataSource.
      Have you done this?
      >
      Marc
      >

      Comment

      Working...