I'm using .NET 2.0 and I'm trying to bind a combobox's Datasource to a sorted dictionary.
So the error I'm getting is "DataMember property 'Key' cannot be found on the Datasource".
SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache();
userListComboBox.DataSource = new BindingSource(userCache, "Key"); //This line is causing the error
userListComboBox.DisplayMember = "Key";
userListComboBox.ValueMember = "Value";
SortedDictionary<string, int> userCache = new SortedDictionary<string, int>
{
{"a", 1},
{"b", 2},
{"c", 3}
};
comboBox1.DataSource = new BindingSource(userCache, null);
comboBox1.DisplayMember = "Key";
comboBox1.ValueMember = "Value";
But why are you setting the ValueMember to "Value", shouldn't it be bound to "Key" (and DisplayMember to "Value" as well)?
I used Sorin Comanescu's solution, but hit a problem when trying to get the selected value. My combobox was a toolstrip combobox. I used the "combobox" property, which exposes a normal combobox.
I had a
Dictionary<Control, string> controls = new Dictionary<Control, string>();
Binding code (Sorin Comanescu's solution - worked like a charm):
controls.Add(pictureBox1, "Image");
controls.Add(dgvText, "Text");
cbFocusedControl.ComboBox.DataSource = new BindingSource(controls, null);
cbFocusedControl.ComboBox.ValueMember = "Key";
cbFocusedControl.ComboBox.DisplayMember = "Value";
The problem was that when I tried to get the selected value, I didn't realize how to retrieve it. After several attempts I got this:
var control = ((KeyValuePair<Control, string>) cbFocusedControl.ComboBox.SelectedItem).Key
Hope it helps someone else!
var colors = new Dictionary < string, string > ();
colors["10"] = "Red";
Binding to Combobox
comboBox1.DataSource = new BindingSource(colors, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
Full Source...Dictionary as a Combobox Datasource
Jeryy
userListComboBox.DataSource = userCache.ToList();
userListComboBox.DisplayMember = "Key";
A dictionary cannot be directly used as a data source, you should do more.
SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache();
KeyValuePair<string, int> [] ar= new KeyValuePair<string,int>[userCache.Count];
userCache.CopyTo(ar, 0);
comboBox1.DataSource = ar; new BindingSource(ar, "Key"); //This line is causing the error
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
I know this is a pretty old topic, but I also had a same problem.
My solution:
how we fill the combobox:
foreach (KeyValuePair<int, string> item in listRegion)
{
combo.Items.Add(item.Value);
combo.ValueMember = item.Value.ToString();
combo.DisplayMember = item.Key.ToString();
combo.SelectedIndex = 0;
}
and that's how we get inside:
MessageBox.Show(combo_region.DisplayMember.ToString());
I hope it help someone
If this doesn't work why not simply do a foreach loop over the dictionary adding all the items to the combobox?
foreach(var item in userCache)
{
userListComboBox.Items.Add(new ListItem(item.Key, item.Value));
}
Use -->
comboBox1.DataSource = colors.ToList();
Unless the dictionary is converted to list, combo-box can't recognize its members.
Just Try to do like this....
SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache();
// Add this code
if(userCache != null)
{
userListComboBox.DataSource = new BindingSource(userCache, null); // Key => null
userListComboBox.DisplayMember = "Key";
userListComboBox.ValueMember = "Value";
}
I am two Combobox which are populated with a view of a table like following
recieverComboBox.DataSource = myDataSet.Tables[0].DefaultView;
recieverComboBox.DisplayMember = "UserPosition";
recieverComboBox.ValueMember = "ID";
recieverUnReadComboBox.DataSource = myDataSet.Tables[0].DefaultView;
recieverUnReadComboBox.DisplayMember = "UserPosition";
recieverUnReadComboBox.ValueMember = "usr_Id";
when I change the value of each of them, the value of the other one changes automatically. why is that ?
Here are two ways to avoid this issue.
Solution A:
Use BindingSource as the DataSource of ComboBox.
recieverComboBox.DataSource = new BindingSource(myDataSet.Tables[0].DefaultView, null);
recieverComboBox.DisplayMember = "UserPosition";
recieverComboBox.ValueMember = "ID";
recieverUnReadComboBox.DataSource = new BindingSource(myDataSet.Tables[0].DefaultView, null);
recieverUnReadComboBox.DisplayMember = "UserPosition";
recieverUnReadComboBox.ValueMember = "usr_Id";
Solution B:
Call method DataTable.Copy
recieverComboBox.DataSource = myDataSet.Tables[0].Copy();
recieverComboBox.DisplayMember = "UserPosition";
recieverComboBox.ValueMember = "ID";
recieverUnReadComboBox.DataSource = myDataSet.Tables[0].Copy();
recieverUnReadComboBox.DisplayMember = "UserPosition";
recieverUnReadComboBox.ValueMember = "usr_Id";
To follow my previous post here => Binding SelectedItem of ComboBox in DataGrid with different type
I have now a datagrid containing 2 columns, one with a text, the other with a combobox (in a datatemplate, written thru the C# code, not the Xaml).
After having done some choice on the combobox, I now would like to parse the result but the value of the cell containing my combobox stay empty :
foreach(DataRowView row in Datagrid1.Items)
{
var firstColumNresult = row.Row.ItemArray[0];// Return correctly a string
var myrow = row.Row.ItemArray[1];// always empty...
}
The result is that I cant get the values of my (previously generated) combobox.
I suppose one binding must missed somewhere...
This is the combobox creation code :
DataTable tableForDG = new DataTable();
tableForDG.Columns.Add(new DataColumn { ColumnName = "Name", Caption = "Name" });
tableForDG.Columns.Add(new DataColumn { ColumnName = "Attachment", Caption = "Attachment" }); // this column will be replaced
tableForDG.Columns.Add(new DataColumn { ColumnName = "AttachmentValue", Caption = "AttachmentValue" });
tableForDG.Columns.Add(new DataColumn { ColumnName = "DisplayCombo", Caption = "DisplayCombo", DataType=bool });
// Populate dataview
DataView myDataview = new DataView(tableForDG);
foreach (var value in listResults)// a list of string
{
DataRowView drv = myDataview.AddNew();
drv["Name"] = value.Name;
drv["Attachment"] = value.Name;// this column will be replaced...
drv["DisplayCombo"] = true;// but it can be false on my code...
}
var DG = myDataview;//
Datagrid1.ItemsSource = DG;
Datagrid1.AutoGenerateColumns = true;
Datagrid1.Items.Refresh();
DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
dgTemplateColumn.Header = "Attachment";
var newCombobox = new FrameworkElementFactory(typeof(ComboBox));
newCombobox.SetValue(ComboBox.NameProperty, "myCBB");
Binding enableBinding = new Binding();
newCombobox.SetValue(ComboBox.IsEnabledProperty, new Binding("DisplayCombo"));
newCombobox.SetValue(ComboBox.SelectedValueProperty, new Binding("AttachmentValue"));
List<string> listUnitAlreadyAttached = new List<string>();
// fill the list...
enableBinding.Source = listUnitAlreadyAttached;
newCombobox.SetBinding(ComboBox.ItemsSourceProperty, enableBinding);
var dataTplT = new DataTemplate();
dataTplT.VisualTree = newCombobox;
dgTemplateColumn.CellTemplate = dataTplT;
Datagrid1.Columns[1] = dgTemplateColumn;
Any idea/advice ?
You should explicitely specify the binding mode and update trigger of your binding. Also use SetBinding instead of SetValue:
var valueBinding = new Binding("AttachmentValue")
{
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
newCombobox.SetBinding(ComboBox.SelectedValueProperty, valueBinding);
This should enable you to get the selected value into your row data. It might not update in the displayed datagrid value for the AttachmentValue column.
I need to create a combobox autocomplete which display text Name but when I click on text it gets value "ID" binding with "Name". I have already created a code but it is not working and I'm so confusing with set display text and value into combobox and autocomplete data-source binding.
private void loadAutoCompleteValues()
{
autoCompleteCombo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
autoCompleteCombo.AutoCompleteSource = AutoCompleteSource.CustomSource;
DataTable products;
con.MysqlQuery("select * from products");
products = con.QueryEx();
Dictionary<string, string> comboSource = new Dictionary<string, string>();
for (int i = 0; i < products.Rows.Count; i++)
{
DataRow dr = products.Rows[i];
comboSource.Add(dr["id"].ToString(), dr["name"].ToString());
}
autoCompleteCombo.DataSource = new BindingSource(comboSource, null);
autoCompleteCombo.DisplayMember = "Value";
autoCompleteCombo.ValueMember = "Key";
}
private void autoCompleteCombo_SelectedIndexChanged(object sender, EventArgs e)
{
string key = ((KeyValuePair<string, string>)autoCompleteCombo.SelectedItem).Key;
string value = ((KeyValuePair<string, string>)autoCompleteCombo.SelectedItem).Value;
MessageBox.Show(key + " " + value);
}
I may be incorrect here, however using your code I simply added the line autoCompleateCombo.AutoCompleteSource = AutoCompleteSource.ListItems; to your code and it worked as expected.
autoCompleateCombo.DataSource = new BindingSource(comboSource, null);
autoCompleateCombo.DisplayMember = "Value";
autoCompleateCombo.ValueMember = "Key";
autoCompleateCombo.AutoCompleteSource = AutoCompleteSource.ListItems; //<-- Added this line
I have a method (LoadCustomers()) that returns a dictionary of elements as follows:
Dictionary<int, string>()
I connected it to the datasource of a combobox like this:
Myclass m = new Myclass();
combo1.DataSource = new BindingSource(m.LoadCustomers(), null);
combo1.DisplayMember = "Value";
combo1.ValueMember = "Key";
Now I would like to put in front of the list of the combobox an item like:
<select one customer>
How to do this in c# on winforms?
Tks a lot
Add this option to the Dictionary of customers
const int EMPTYCUSTOMERKEY = -1; //be sure Customers will not contain this value
const string EMPTYCUSTOMERVALUE = "<select one customer>";
Myclass m = new Myclass();
Dictionary<int, string> customerSource = m.LoadCustomers();
customerSource.Add(EMPTYCUSTOMERKEY, EMPTYCUSTOMERVALUE);
combo1.DataSource = new BindingSource(customerSource, null);
combo1.DisplayMember = "Value";
combo1.ValueMember = "Key";