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. Binded ComboBox displays wrong item!

Binded ComboBox displays wrong item!

Scheduled Pinned Locked Moved C#
databasewpfwcfquestion
8 Posts 3 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.
  • G Offline
    G Offline
    goldoche
    wrote on last edited by
    #1

    Hi, I'm trying to bind a lookup table to a comboBox. Well the contents of the ComboBox binds ok, but the binding on the SelectedValue doesn't work! Here's my code: // Build lookup table: (name => index) (IT WORKS) DataTable table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Value", typeof(int)); for(int i = 0; i < namesArray.Length; ++i) { table.Rows.Add(new object[] {namesArray[i], i}); } // The newly created table is binded to the contents of the ComboBox (IT WORKS) combo.DataSource = table; combo.DisplayMember = "Name"; combo.ValueMember = "Value"; // The SelectedValue of the comboBox is binded to a column in the LoadModule Table // And it DOES NOT WORK! combo.DataBindings.Add("SelectedValue", dataset.LoadModule, "MyColumn"); // The following DOES WORK numericUpDown1.DataBindings.Add("Value", dataset.LoadModule, "MyColumn"); What am I missing? This is going to drive me nuts!!!

    C L 4 Replies Last reply
    0
    • G goldoche

      Hi, I'm trying to bind a lookup table to a comboBox. Well the contents of the ComboBox binds ok, but the binding on the SelectedValue doesn't work! Here's my code: // Build lookup table: (name => index) (IT WORKS) DataTable table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Value", typeof(int)); for(int i = 0; i < namesArray.Length; ++i) { table.Rows.Add(new object[] {namesArray[i], i}); } // The newly created table is binded to the contents of the ComboBox (IT WORKS) combo.DataSource = table; combo.DisplayMember = "Name"; combo.ValueMember = "Value"; // The SelectedValue of the comboBox is binded to a column in the LoadModule Table // And it DOES NOT WORK! combo.DataBindings.Add("SelectedValue", dataset.LoadModule, "MyColumn"); // The following DOES WORK numericUpDown1.DataBindings.Add("Value", dataset.LoadModule, "MyColumn"); What am I missing? This is going to drive me nuts!!!

      C Offline
      C Offline
      Christopher Stratmann
      wrote on last edited by
      #2

      I am thinking that you may have a sorting problem. Do you have the combo box sort set to accending or decending. If so I think that is your problem. Try the following after adding all the rows... table.DefaultView.Sort = "Name"; combo.DataSource = table.DefaultView; Chris

      1 Reply Last reply
      0
      • G goldoche

        Hi, I'm trying to bind a lookup table to a comboBox. Well the contents of the ComboBox binds ok, but the binding on the SelectedValue doesn't work! Here's my code: // Build lookup table: (name => index) (IT WORKS) DataTable table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Value", typeof(int)); for(int i = 0; i < namesArray.Length; ++i) { table.Rows.Add(new object[] {namesArray[i], i}); } // The newly created table is binded to the contents of the ComboBox (IT WORKS) combo.DataSource = table; combo.DisplayMember = "Name"; combo.ValueMember = "Value"; // The SelectedValue of the comboBox is binded to a column in the LoadModule Table // And it DOES NOT WORK! combo.DataBindings.Add("SelectedValue", dataset.LoadModule, "MyColumn"); // The following DOES WORK numericUpDown1.DataBindings.Add("Value", dataset.LoadModule, "MyColumn"); What am I missing? This is going to drive me nuts!!!

        C Offline
        C Offline
        Christopher Stratmann
        wrote on last edited by
        #3

        Also make sure you set the sort on the combobox to none.

        1 Reply Last reply
        0
        • G goldoche

          Hi, I'm trying to bind a lookup table to a comboBox. Well the contents of the ComboBox binds ok, but the binding on the SelectedValue doesn't work! Here's my code: // Build lookup table: (name => index) (IT WORKS) DataTable table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Value", typeof(int)); for(int i = 0; i < namesArray.Length; ++i) { table.Rows.Add(new object[] {namesArray[i], i}); } // The newly created table is binded to the contents of the ComboBox (IT WORKS) combo.DataSource = table; combo.DisplayMember = "Name"; combo.ValueMember = "Value"; // The SelectedValue of the comboBox is binded to a column in the LoadModule Table // And it DOES NOT WORK! combo.DataBindings.Add("SelectedValue", dataset.LoadModule, "MyColumn"); // The following DOES WORK numericUpDown1.DataBindings.Add("Value", dataset.LoadModule, "MyColumn"); What am I missing? This is going to drive me nuts!!!

          C Offline
          C Offline
          Christopher Stratmann
          wrote on last edited by
          #4

          If that doesn't work then try combo.DataBindings.Add("SelectedValue", table /*or dataset.Tables["MyTableName"]*/, "Value");

          G 1 Reply Last reply
          0
          • C Christopher Stratmann

            If that doesn't work then try combo.DataBindings.Add("SelectedValue", table /*or dataset.Tables["MyTableName"]*/, "Value");

            G Offline
            G Offline
            goldoche
            wrote on last edited by
            #5

            Thank you for the help! Now the right item is selected, but when I change it, it doesn't change the data source! For example: private void myCombo_SelectedValueChanged(object sender, System.EventArgs e) { // cm is my CurrencyManager. MessageBox.Show(dataset.LoadModule[cm.Position].MyColumn.ToString()); } In this code above, the MessageBox always displays '5', which is the initial SelectedValue. I'm really having a hard time with this!

            C 1 Reply Last reply
            0
            • G goldoche

              Thank you for the help! Now the right item is selected, but when I change it, it doesn't change the data source! For example: private void myCombo_SelectedValueChanged(object sender, System.EventArgs e) { // cm is my CurrencyManager. MessageBox.Show(dataset.LoadModule[cm.Position].MyColumn.ToString()); } In this code above, the MessageBox always displays '5', which is the initial SelectedValue. I'm really having a hard time with this!

              C Offline
              C Offline
              Christopher Stratmann
              wrote on last edited by
              #6

              I dont have much experience with a currency manager. I am guessing the same datatable isn't getting bound to the currency manager. How are you binding the code to the currency manager?

              G 1 Reply Last reply
              0
              • C Christopher Stratmann

                I dont have much experience with a currency manager. I am guessing the same datatable isn't getting bound to the currency manager. How are you binding the code to the currency manager?

                G Offline
                G Offline
                goldoche
                wrote on last edited by
                #7

                I use the CurrencyManager because its Position property represents the index of the current record being viewed. FYI this is how I use it and it works fine: cm = BindingContext[dataset.LoadModule] as CurrencyManager; dataset.LoadModule.DefaultView.Sort = "SerialNumber"; cm.Position = dataset.LoadModule.DefaultView.Find(serialNumber); But this is not a CurrencyManager issue, since the program gives me the right value (5), it just doesn't update it!

                1 Reply Last reply
                0
                • G goldoche

                  Hi, I'm trying to bind a lookup table to a comboBox. Well the contents of the ComboBox binds ok, but the binding on the SelectedValue doesn't work! Here's my code: // Build lookup table: (name => index) (IT WORKS) DataTable table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Value", typeof(int)); for(int i = 0; i < namesArray.Length; ++i) { table.Rows.Add(new object[] {namesArray[i], i}); } // The newly created table is binded to the contents of the ComboBox (IT WORKS) combo.DataSource = table; combo.DisplayMember = "Name"; combo.ValueMember = "Value"; // The SelectedValue of the comboBox is binded to a column in the LoadModule Table // And it DOES NOT WORK! combo.DataBindings.Add("SelectedValue", dataset.LoadModule, "MyColumn"); // The following DOES WORK numericUpDown1.DataBindings.Add("Value", dataset.LoadModule, "MyColumn"); What am I missing? This is going to drive me nuts!!!

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  AFAIK there is no ComboBox.SelectedValue did you mean SelectedText ? :)

                  Luc Pattyn [My Articles] [Forum Guidelines]

                  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