C # Re-populating a dictionary after resetting / setting it to zero

I have a dictionary populated with KeyValuePairs (equalMap), which I use to populate combobox (comBox1).

I want to call the function below as part of comBox1 initialization. Then I have a selectedValueChanged event from another combobox (comBox2) that calls the function below and changes the contents of comBox1 depending on the selected value of comBox2.

Everything works as expected when a join with a hit list is called first. However, when this function is called again, instead of showing only the "key" in the drop-down list, it displays the "key" and "value" in the format ["key", "value"]

I just started with C # (or something with a GUI), so I donโ€™t know how best to debug something like that. Any help appreciated.

public void popEqualities(String fieldType) { this.equalities.DataSource = null; this.equalities.Items.Clear(); this.equalityMap.Clear(); if (fieldType == "string") { equalityMap.Add("is", "="); equalityMap.Add("is not", "!="); equalityMap.Add("contains", "CONTAINS"); equalityMap.Add("begins with", "LIKE '%"); } else if (fieldType == "int") { equalityMap.Add("is equal to", "="); equalityMap.Add("is not equal to", "!="); equalityMap.Add("is greater than", ">"); equalityMap.Add("is less than", "<"); } else if (fieldType == "date") { equalityMap.Add("is", "="); equalityMap.Add("is not", "!="); equalityMap.Add("is after", ">"); equalityMap.Add("is before", "<"); } else if (fieldType == "boolean") { equalityMap.Add("is", "="); } else { MessageBox.Show("Recieved bad Field Type"); return; } this.equalities.DisplayMember = "Key"; this.equalities.ValueMember = "Value"; this.equalities.DataSource = new BindingSource(equalityMap, null); } 

EDIT: declare a map of justice, which I call

 this.equalityMap = new Dictionary<string, string>(); 

in the class constructor and as a private member of the class has the following.

 private Dictionary<string, string> equalityMap 

The event calling this function is simply

 public void searchFieldChanged(object sender, EventArgs e) { string fieldType = getFieldType(); popEqualities(fieldType); } 

Here are some photos to show the problem on first call

initial call .

On subsequent calls

subsequent calls .

Fixed:

It turns out that when I rewrote the DataSource, it cleared the DisplayMember property every time -

 this.equalities.DisplayMember = "Key"; 

When you move a string binding a Datasource above these destinations, it fixes the problem.

 this.equalities.DataSource = new BindingSource(equalityMap, null); this.equalities.DisplayMember = "Key"; this.equalities.ValueMember = "Value"; 
+4
source share
1 answer

The entry in System.Collections.Generic.Dictionary contains the Key and Value properties to display the content. If you just see the record, you implicitly use the ToString() method, which displays the contents of the record as ["key", "value"] .

If you want to show only keys, you must use Key -perperty and print this.

See the MSDN and methods / properties of System.Collections.Generic.Dictionary<TKey, TValue> .

0
source

All Articles