Binded ComboBox displays wrong item!
-
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!!! -
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!!!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
-
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!!!Also make sure you set the sort on the combobox to none.
-
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!!!If that doesn't work then try combo.DataBindings.Add("SelectedValue", table /*or dataset.Tables["MyTableName"]*/, "Value");
-
If that doesn't work then try combo.DataBindings.Add("SelectedValue", table /*or dataset.Tables["MyTableName"]*/, "Value");
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! -
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!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?
-
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?
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! -
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!!!AFAIK there is no ComboBox.SelectedValue did you mean SelectedText ? :)
Luc Pattyn [My Articles] [Forum Guidelines]