Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a Dictionary for the DataSource with a ComboBox, DataBinding to an Integer
    primarykey
    data
    text
    <p>This question has been asked and answered a 100 times by several people. However, I still seem to have an issue. I have a ComboBox that I have a DataBinding to a column in the database that is an Integer. I have a Dictionary class that populates the ComboBox DataSource. When I run the form, either nothing displays in the ComboBox or the Integer value from the database is displayed, not the "Text" from the Dictionary. I have attached picture of the what the form looks like;</p> <p><img src="https://i.stack.imgur.com/cycDD.png" alt="ComboBox display"></p> <p>Here is the code used to generate this ComboBox;</p> <pre><code> public static SortedDictionary&lt;int, string&gt; SecurityCodes() { SortedDictionary&lt;int, string&gt; SecurityCodes = new SortedDictionary&lt;int, string&gt; { {1, "View only"}, {3, "Print reports"}, {4, "Post changes"}, {5, "Edit records"}, {6, "Revise postings"}, {7, "Add records"}, {8, "Remove postings"}, {9, "Supervisor"}, {10, "Programmer"} }; return SecurityCodes; } this.cboSecurity.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.loginInfoBindingSource, "Security_Level", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged)); this.cboSecurity.DataSource = new BindingSource(BaseDictionaries.SecurityCodes(), null); this.cboSecurity.DisplayMember = "Value"; this.cboSecurity.ValueMember = "Key"; </code></pre> <p>Now if I change the DataBinding to "ValueMember" instead of "Text", the text will display, but it does not update when you navigate to another record, it always remains on the first item of the Dictionary. If I change the DataBinding to a View that has matching entries for what is in the Dictionary, then the appropriate "Text" is displayed in the ComboBox with the DataBinding set to "Text". Which is okay, but then I have to maintain a table in the database with just these entries and I would prefer not to go that route.</p> <p>Can anyone see what I done wrong?</p> <p>I have tried many different types of DataSource combinations, including the following, with no change in the behavior;</p> <pre><code> SortedDictionary&lt;int, string&gt; loDataSource = BaseDictionaries.SecurityCodes(); KeyValuePair&lt;int, string&gt;[] kvp = new KeyValuePair&lt;int,string&gt;[loDataSource.Count]; loDataSource.CopyTo(kvp,0); new BindingSource(kvp, "Key"); this.cboSecurity.DataSource = kvp; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload