ComboBox and DataSet binding
-
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
-
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
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