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. Windows Forms
  4. ComboBox and DataSet binding

ComboBox and DataSet binding

Scheduled Pinned Locked Moved Windows Forms
tutorialwpfwcfquestiondiscussion
2 Posts 2 Posters 8 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.
  • G Offline
    G Offline
    gotdablues
    wrote on last edited by
    #1

    I have a Windows Form app that contains a combobox (as a dropdownlist). I am able to populate the combobox. However, I am seeing some strange behaviour when I try to set the SelectedValue to an item in the list. This is how I populate my combobox: Method 1. This works. // The "" indicates no custom first row. GetList() returns DataSet. cbState.DataSource = GetList("spGetStates", ""); cbState.DisplayMember = "Text"; cbState.ValueMember = "Value"; cbState.SelectedValue = "AL" // This positions correctly Method 2. This does NOT work. // The "--" indicates first row in combo will contain this value. GetList() returns DataSet. cbState.DataSource = GetList("spGetStates", "--"); cbState.DisplayMember = "Text"; cbState.ValueMember = "Value"; cbState.SelectedValue = "AL" // This does NOT position correctly. The only difference between Method 1 and 2 is that Method 2 inserts a row into the dataset. If I were to use "AK" in method 2, it would work correctly. There is something about the second row in table. Here is the method that populates DataSet: public static GetList(string spName, string FirstRow) { // Populate dataset. Some code left out to keep it simple ds = ....; if (FirstRow.Length > 0) { DataRow row = new DataRow(); row["Value"] = FirstRow; row["Text"] = FirstRow; // Add row to top of list ds.Tables[0].Row.InsertAt(row, 0) } } Here is some sample data: EXAMPLE 1 (using "--" parameter) value text ------------ -- -- AL Alabama AK Alaska EXAMPLE 2 (using "" parameter) value text ------------ AL Alabama AK Alaska Any thoughts on how to get the positioning to work for Method 2 using the value "AL"? Thanks. Steve

    K 1 Reply Last reply
    0
    • G gotdablues

      I have a Windows Form app that contains a combobox (as a dropdownlist). I am able to populate the combobox. However, I am seeing some strange behaviour when I try to set the SelectedValue to an item in the list. This is how I populate my combobox: Method 1. This works. // The "" indicates no custom first row. GetList() returns DataSet. cbState.DataSource = GetList("spGetStates", ""); cbState.DisplayMember = "Text"; cbState.ValueMember = "Value"; cbState.SelectedValue = "AL" // This positions correctly Method 2. This does NOT work. // The "--" indicates first row in combo will contain this value. GetList() returns DataSet. cbState.DataSource = GetList("spGetStates", "--"); cbState.DisplayMember = "Text"; cbState.ValueMember = "Value"; cbState.SelectedValue = "AL" // This does NOT position correctly. The only difference between Method 1 and 2 is that Method 2 inserts a row into the dataset. If I were to use "AK" in method 2, it would work correctly. There is something about the second row in table. Here is the method that populates DataSet: public static GetList(string spName, string FirstRow) { // Populate dataset. Some code left out to keep it simple ds = ....; if (FirstRow.Length > 0) { DataRow row = new DataRow(); row["Value"] = FirstRow; row["Text"] = FirstRow; // Add row to top of list ds.Tables[0].Row.InsertAt(row, 0) } } Here is some sample data: EXAMPLE 1 (using "--" parameter) value text ------------ -- -- AL Alabama AK Alaska EXAMPLE 2 (using "" parameter) value text ------------ AL Alabama AK Alaska Any thoughts on how to get the positioning to work for Method 2 using the value "AL"? Thanks. Steve

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      I am not totally sure I understand the question, but you can not set the selected item by setting the selectedValue or selectedText. You can only set the selected Item by using the SelectedIndex and SelectedItem. If you want to select AL then you would need to do something like: You might need to loop through the comboBox to check the value and find the index. cbState.SelectedIndex = columnIdx Hope that helps. Ben

      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