Get the value of the selected item of a combobox [

csharp United States
  • 13 years ago

    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

  • 13 years ago
    Each Item in the ComboBox is a DataRowView, so the SelectedItem property is a DataRowView.  The reason you set a ValueMember for the ComboBox is so that you can use the SelectedValue property.

    Also, there's no point setting the SelectedIndex to -1 before setting the DataSource.  You need to do it afterwards if you want no Item selected.  Also there is a bug in the ComboBox that requires you to do it twice when it is data-bound.
  • 13 years ago
    If you choose to use the ValueMember to get an ID rather than a name or the like, as I just suggested in one of your other threads, you can get the displayed value of the SelectedItem like this:Code:
    DirectCast(myComboBox.Selected, DataRowView)(myComboBox.DisplayMember)
    This gets the value in the displayed column from the selected row.

    If it is a string value to begin with then you can get it from the Text property as well.
  • 13 years ago

    Not 100% sure I follow what you mean but I am very curious...
    My DataSource comes from and EXCEL file (.xls) and therefore there are no "real" column names...
    The XLS file is called "Task" and row1 has headers in bold for each column (CLIENTS, ASSIGNMENTS, ELAPSED TIME) - so I do a select * on that and set the resulting dataset as datasource for the combobox.

    Now, unless I use .DisplayMember="CLIENTS" and .ValueMember="CLIENTS" it doesn't seem to work (or I have not tested every possible combination).

    So, how can using .ValueMember / .SelectedValue to get this to work?

  • 13 years ago

    Where did "CLIENTS" come from?  I assume that you didn't just pluck it out of thin air?  You are binding your control to a DataTable so it neither knows nor cares where the data originated.  If your connection string specified that there was a header row (HDR=YES I believe) then the first row of the sheet has been used as the column names.  If not, then you can easily specify column names yourself, although the fact that you are using "CLIENTS" as a column name would suggest that you used a header row.

  • 13 years ago
    Okay - now you raise a couple more question...
    1- what is an HDR?
    I mean, my XLS File [TASKS.XLS] has one sheet called [TASKS] which looks like the following:
    row1: Client#1 Assignment#1 PAUSED
    row2: Client#2 Assignment#1 PAUSED
    row3: Client#2 Assignment#2 STOPPED
    ... etc ...

    So all I did was "select * from [Tasks$]" to a dataset and this gave me all the data from the table.
    So #1 - C# automatically assumed row0 was the header/column names ????
    #2 - how do I use this HDR you mentioned?
    #3 - how does this affect ValueMembers?

    Now that u know my setup can you tell me how it SHOULD be (using DisplayMember and ValueMember correctly)
    Note that the way I have it seems to work perfectly fine - but I would like to use these properly.
  • 13 years ago

    As I said in my previous post, it is in the connection string that you specify whether the sheet contains a header row or not.  Have a look on MSDN for information on using ADO.NET with Excel to see how to use it, or even  Perhaps if you don't specify it, it isassumed that the first row is the column headers, but I'm guessing that you did specify it but just didn't realise that you were.  Did you just cut and paste a connection string from somewhere?  Either way, given that you are specifying "CLIENTS" as a column name in your code and "CLIENTS" is the value in the first cell, the first row must be being used as the column headers, so the other two columns of your DataTable are named "ASSIGNMENT" and "STATUS".

    Having said all that, you have no ID column so you should keep doing what you're doing and setting the "CLIENTS" column as the ValueMember and use SelectedValue to get the selected client from the ComboBox

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC. As potential programmers, they are mentally mutilated beyond hope of regeneration.” - E. W. Dijkstra