Databound ComboBox Selection Problems
-
I am displaying a ComboBox that hopefully contains the contents of a database table (a lookup of sorts). When the form loads, the ComboBox contains the correct values, all sorted. :) However, when I try to select a value programitically, I can't (well, I can't select the correct one). :( I create a dataset:
DataSet dsTitles = new DataSet("Titles"); SqlCommand tableCommand = new SqlCommand(); tableCommand.CommandText = "select TitleID, TitleDisplayText FROM tblTitles WHERE TitleHidden <> 1"; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = tableCommand; adapter.Fill(dsTitles, "Titles");
I then bind the dataset to the ComboBox:comboBoxTitle.BeginUpdate(); comboBoxTitle.DataSource = db.DataSetTitles.Tables["Titles"]; comboBoxTitle.DisplayMember = "TitleDisplayText"; comboBoxTitle.ValueMember = "TitleID"; comboBoxTitle.EndUpdate();
So far, all is well. However, I can't seem to select the correct item. I've tried:comboBoxTitle.SelectedValue = 2; Console.WriteLine("Current Title Index: {0} Text: {1} Combo Index: {2} Value: {3} Text: {4}", 2, db.LookupValueTitle(2), comboBoxTitle.SelectedIndex, comboBoxTitle.SelectedValue);
TitleID of 2 matches TitleDisplayText of Mr, but nothing is visibily selected.Current Title Index: 2 Text: Mr Combo Index: 0 Value: 2 Text:
is what gets logged. Any help would be appreciated. --G :confused:
-
I am displaying a ComboBox that hopefully contains the contents of a database table (a lookup of sorts). When the form loads, the ComboBox contains the correct values, all sorted. :) However, when I try to select a value programitically, I can't (well, I can't select the correct one). :( I create a dataset:
DataSet dsTitles = new DataSet("Titles"); SqlCommand tableCommand = new SqlCommand(); tableCommand.CommandText = "select TitleID, TitleDisplayText FROM tblTitles WHERE TitleHidden <> 1"; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = tableCommand; adapter.Fill(dsTitles, "Titles");
I then bind the dataset to the ComboBox:comboBoxTitle.BeginUpdate(); comboBoxTitle.DataSource = db.DataSetTitles.Tables["Titles"]; comboBoxTitle.DisplayMember = "TitleDisplayText"; comboBoxTitle.ValueMember = "TitleID"; comboBoxTitle.EndUpdate();
So far, all is well. However, I can't seem to select the correct item. I've tried:comboBoxTitle.SelectedValue = 2; Console.WriteLine("Current Title Index: {0} Text: {1} Combo Index: {2} Value: {3} Text: {4}", 2, db.LookupValueTitle(2), comboBoxTitle.SelectedIndex, comboBoxTitle.SelectedValue);
TitleID of 2 matches TitleDisplayText of Mr, but nothing is visibily selected.Current Title Index: 2 Text: Mr Combo Index: 0 Value: 2 Text:
is what gets logged. Any help would be appreciated. --G :confused:
Try to use
comboBoxTitle.Text
insted ofcomboBoxTitle.SelectedValue
:-D Sreejith Nair [ My Articles ] -
I am displaying a ComboBox that hopefully contains the contents of a database table (a lookup of sorts). When the form loads, the ComboBox contains the correct values, all sorted. :) However, when I try to select a value programitically, I can't (well, I can't select the correct one). :( I create a dataset:
DataSet dsTitles = new DataSet("Titles"); SqlCommand tableCommand = new SqlCommand(); tableCommand.CommandText = "select TitleID, TitleDisplayText FROM tblTitles WHERE TitleHidden <> 1"; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = tableCommand; adapter.Fill(dsTitles, "Titles");
I then bind the dataset to the ComboBox:comboBoxTitle.BeginUpdate(); comboBoxTitle.DataSource = db.DataSetTitles.Tables["Titles"]; comboBoxTitle.DisplayMember = "TitleDisplayText"; comboBoxTitle.ValueMember = "TitleID"; comboBoxTitle.EndUpdate();
So far, all is well. However, I can't seem to select the correct item. I've tried:comboBoxTitle.SelectedValue = 2; Console.WriteLine("Current Title Index: {0} Text: {1} Combo Index: {2} Value: {3} Text: {4}", 2, db.LookupValueTitle(2), comboBoxTitle.SelectedIndex, comboBoxTitle.SelectedValue);
TitleID of 2 matches TitleDisplayText of Mr, but nothing is visibily selected.Current Title Index: 2 Text: Mr Combo Index: 0 Value: 2 Text:
is what gets logged. Any help would be appreciated. --G :confused:
Sreejith's solution should work. Also keep in mind http://support.microsoft.com/kb/327244
-
Try to use
comboBoxTitle.Text
insted ofcomboBoxTitle.SelectedValue
:-D Sreejith Nair [ My Articles ]Thanks. Tried that -- didn't work. I fixed it by removing the comboBox from the form, then adding it back. Don't understand, but problem solved.