Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with ComboBox.SelectedValue

Problem with ComboBox.SelectedValue

Scheduled Pinned Locked Moved C#
databasesaleshelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leppie
    wrote on last edited by
    #1

    Hi all, ComboBox.SelectedValue dont seem to work correctly. Code:

    ...
    void SetupCustomers(){ //called from form constructor
    cbCust.DataSource = dbase.GetCustomers(); //returns Customer[] from database
    cbCust.DisplayMember = "Name"; // The name property
    cbCust.ValueMember = "ID"; // The ID property (PK in database)
    }
    ...

    However when calling cbCust.SelectedValue returns the wrong value. Calling ((Customer)cbCust.SelectedItem).ID gives the correct answer. This seems to be a problem with the DisplayMember being (automatically) sorted as all the returned SelectedValue's are within range. EG : Customers[] expanded may look like this: ID(Value) Name(Display) 20 Ben 21 John 23 Ann 26 Frank 27 Arnold The combobox will display the Displaymembers in alphebetical order. Here is where the problem lies (i think :)): Value Display 20 Ann 21 Arnold 23 Ben 26 Frank 27 John Now this is how Mr. ComboBox display and intepret Value and Display members, thus getting the mappings all wrong. If anyone has had a similar experience or can spot where I made a mistake, please tell me :) Thanking all replies in advance Cheers :) READ MSDN

    C 1 Reply Last reply
    0
    • L leppie

      Hi all, ComboBox.SelectedValue dont seem to work correctly. Code:

      ...
      void SetupCustomers(){ //called from form constructor
      cbCust.DataSource = dbase.GetCustomers(); //returns Customer[] from database
      cbCust.DisplayMember = "Name"; // The name property
      cbCust.ValueMember = "ID"; // The ID property (PK in database)
      }
      ...

      However when calling cbCust.SelectedValue returns the wrong value. Calling ((Customer)cbCust.SelectedItem).ID gives the correct answer. This seems to be a problem with the DisplayMember being (automatically) sorted as all the returned SelectedValue's are within range. EG : Customers[] expanded may look like this: ID(Value) Name(Display) 20 Ben 21 John 23 Ann 26 Frank 27 Arnold The combobox will display the Displaymembers in alphebetical order. Here is where the problem lies (i think :)): Value Display 20 Ann 21 Arnold 23 Ben 26 Frank 27 John Now this is how Mr. ComboBox display and intepret Value and Display members, thus getting the mappings all wrong. If anyone has had a similar experience or can spot where I made a mistake, please tell me :) Thanking all replies in advance Cheers :) READ MSDN

      C Offline
      C Offline
      Chris Rickard
      wrote on last edited by
      #2

      I don't suppose you're setting the Sort property on the combobox? If not maybe an explicit set to false would do the trick

      L 1 Reply Last reply
      0
      • C Chris Rickard

        I don't suppose you're setting the Sort property on the combobox? If not maybe an explicit set to false would do the trick

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        Thanx Chris, however... 1. no the Sort property is not set. 2. but Sorting is really needed. :) Like i said a quick work around does the trick, but i have searched forums and articles with nothing pointing to what i experience. READ MSDN

        C 1 Reply Last reply
        0
        • L leppie

          Thanx Chris, however... 1. no the Sort property is not set. 2. but Sorting is really needed. :) Like i said a quick work around does the trick, but i have searched forums and articles with nothing pointing to what i experience. READ MSDN

          C Offline
          C Offline
          Chris Rickard
          wrote on last edited by
          #4

          I was able to reproduce the behavior doing to following: I made a class: test with properties Name and Id. Set the ComboBox's DataSource to a test[] With Name as DisplayMember and Id as ValueMember Set Sorted=true Noting that I had to set sort before setting the DataSource property otherwise it will throw an exception. In thinking about it the behavior makes sense that it could happen this way. When you set the DataSource of a ComboBox it copies the list to its own internal list. Text returns the item in the internal list. SelectedValue uses a combiniation of CurrenctManager ,SelectedIndex and ValueMember to retrieve an Item from the datasource. SelectedItem is probably a pointer stored in the internal list that gets Sorted with Text. Anyways Sorting your DataSource (via Array.Sort, ArrayList.Sort, DataTable.DefaultView.Sort; whatever the type of DataSource you have) should fix the problem. Even if the Sorted property inadvertantly gets set on the combobox, there's no mismatch between your datasource and its internal list because its already sorted.:)

          L 1 Reply Last reply
          0
          • C Chris Rickard

            I was able to reproduce the behavior doing to following: I made a class: test with properties Name and Id. Set the ComboBox's DataSource to a test[] With Name as DisplayMember and Id as ValueMember Set Sorted=true Noting that I had to set sort before setting the DataSource property otherwise it will throw an exception. In thinking about it the behavior makes sense that it could happen this way. When you set the DataSource of a ComboBox it copies the list to its own internal list. Text returns the item in the internal list. SelectedValue uses a combiniation of CurrenctManager ,SelectedIndex and ValueMember to retrieve an Item from the datasource. SelectedItem is probably a pointer stored in the internal list that gets Sorted with Text. Anyways Sorting your DataSource (via Array.Sort, ArrayList.Sort, DataTable.DefaultView.Sort; whatever the type of DataSource you have) should fix the problem. Even if the Sorted property inadvertantly gets set on the combobox, there's no mismatch between your datasource and its internal list because its already sorted.:)

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            thx, i wrote the original UI code a while back so i'll check if i maybe did set it to sort. my workaround is sufficient for now though :) READ MSDN

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups