Ive got a dictionary filled with KeyValuePairs (equalityMap) which I'm using to populate a combobox (comBox1).
I want to call the function below as a part of initializing comBox1. Then, I have a selectedValueChanged event from another combobox (comBox2) which calls the below function and changes comBox1's content based on the type of comBox2's selected value.
Everything is working as expected when the equalities combobox is first initialized. However, when this function is being called again, instead of just the "key" being displayed in the combobox it displays the "key" and the "value" in the format ["key", "value"]
I've only just started with c# (or anything with a GUI) so unsure of the best way to debug something like this. 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: To declare equity map i call
this.equalityMap = new Dictionary<string, string>();
in the class constructor and have the following as a private member of the class.
private Dictionary<string, string> equalityMap
The event that calls this function is simply
public void searchFieldChanged(object sender, EventArgs e)
{
string fieldType = getFieldType();
popEqualities(fieldType);
}
Here's a couple of pics to show the issue On the initial call
.
On subsequent calls
.
Fixed:
Turns out that when I was rebinding the DataSource it was clearing the DisplayMember property each time -
this.equalities.DisplayMember = "Key";
When you move the line rebinding the Datasource above these assignments it fixes the problem.
this.equalities.DataSource = new BindingSource(equalityMap, null);
this.equalities.DisplayMember = "Key";
this.equalities.ValueMember = "Value";