CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2004

    Question Get the value of the selected item of a combobox [C#]

    Given a combobox (cbClient dropdown) - the user can select one of the items listed in the combobox and when he does I need to know what item he selected.
    The combobox is populated by a DataSet (as shown in the code below), everytime I try to get the value of the selected item I get "System.Data.DataRowView" instead ???

    System.Data.DataSet ds = new System.Data.DataSet();
    ds = Database.Read("select * from [TaskTimer$]");
    cbClient.SelectedIndex = -1;
    cbClient.DataSource = ds.Tables[0];
    cbClient.DisplayMember = "CLIENTS";
    cbClient.ValueMember = "CLIENTS";
    ... The user can do stuff like select a Client from the ComboBox DropDown
    string sSelectedClient = cbClient.SelectedItem.ToString();
    So I am trying to get sSelectedClient to be = the selected client from the DrownDown Combobox.... but thats not what I am getting.
    Do I need to CAST it somehow, into like a DATASET and then extract it as such? If so how would I go about doing that?
    Any help/hints would be greatly appreciated

  2. #2
    Join Date
    Feb 2005

    Re: Get the value of the selected item of a combobox [C#]

    Several Things:
    1. DisplayMember property should be the name of the column to be displayed inside the combobox.
    2. ValueMember property should be the name of the column which is the value of the item.

    For example:
    cbClient.DataSource = ds.Tables["CLIENTS"];
    cbClient.DisplayMember = "NAME";
    cbClient.ValueMember = "ID";
    3. If you want the value of the selected item you should use:
    object sSelectedClient = cbClient.SelectedValue;
    If the value is a string than do:
    string sSelectedClient  = (string) cbClient.SelectedValue;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.