I fill out a ComboBox using below code:
cbxLines.DisplayMember = "Value";
cbxLines.ValueMember = "Key";
cbxLines.DataSource = new BindingSource(GetProductionLines(), null);
private Dictionary<int, string> GetProductionLines()
Now I want to fill out a ListView with every DisplayMember from the ComboBox among other info:
lvSelectedSetup.Items.Clear();
for (int i = 0; i <= cbxLines.Items.Count - 1; i++)
{
ListViewItem item = new ListViewItem();
item.SubItems.Add(cbxLines.Items[i].ToString()); <-- How to Get DisplayMember
item.SubItems.Add(cbxFromDate.Text);
item.SubItems.Add(cbxToDate.Text);
lvSelectedSetup.Items.Add(item);
}
But I don't know how to get either the ValueMember or DisplayMember from the ComboBox.
I was trying doing the following, but get stuck:
item.SubItems.Add(cbxLines.Items[i].GetType().GetProperty(cbxLines.ValueMember).GetValue(cbxLines,null))
Any Advice?
Gets the key in the key/value pair.
((KeyValuePair<int, string>)cbxLines.Items[i]).Key
Gets the value in the key/value pair.
((KeyValuePair<int, string>)cbxLines.Items[i]).Value
Related
I am using Dictionary collection in C#. I want to display first value of dictionary
to combo box so that combo box by default shows first value.but instead of the first value null value get assigned.
I tried following code:
Dictionary<string, string> sampleDictionary = new Dictionary<string, string>();
sampleDictionary.add("ABC","XYZ");
sampleDictionary.add("JKL","PQR");
comboBox.SelectedValue=sampleDictionary.Values.First();
Try comboBox.SelectedIndex = 0;
You should add the values of the dictionary to your comboBox as follows:
Dictionary<string, string> sampleDictionary = new Dictionary<string, string>();
comboBox.DataSource = new BindingSource(sampleDictionary, null);
comboBox.DisplayMember = "Key";
comboBox.ValueMember = "Value";
Then, you could try: comboBox.SelectedIndex = 0;
I have a Dictionary<uint, string> and a ComboBox using the style DropDownList, where I bind this dictionary, like:
comboBox1.DataSource = new BindingSource(myDic, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
Now I would like to be able to select an arbitrary item of my dictionary with a button click, so given the bound dictionary items:
Dictionary<uint, string> myDic = new Dictionary<uint, string>()
{
{ 270, "Name1" },
{ 1037, "Name2" },
{ 1515, "Name3" },
};
I have tried:
comboBox1.SelectedItem = myDic[270];
comboBox1.SelectedText = myDic[270];
comboBox1.SelectedValue = myDic[270];
comboBox1.SelectedItem = 270;
comboBox1.SelectedValue = 270;
But none of the above changed the selected item.
How can I change my current selected item by either the key or value of my datasource?
You can do it with a little extension method I found here
Just put this into an extension class.
public static KeyValuePair<TKey, TValue> GetEntry<TKey, TValue>
(this IDictionary<TKey, TValue> dictionary,
TKey key)
{
return new KeyValuePair<TKey, TValue>(key, dictionary[key]);
}
And then you can just set your item like this
comboBox1.SelectedItem = myDic.GetEntry<uint,string>(1515);
The key to this problem is that you have to set the KeyValuePair (and not just the uint or string value/key).
Hope this helps!
This works sufficiently. Not sure about the performance on very large lists, but you probably don't want a huge ComboBox list either.
foreach (var item in myDic)
comboBox1.Items.Add(item.Value); // Populate the ComboBox by manually adding instead of binding
comboBox1.SelectedIndex = comboBox1.Items.IndexOf(myDic[1037]);
The clear issue with this code is the dismissal of the DataSource Binding, which may or may not be okay with you. Perhaps someone else will provide a better answer.
Yesterday looking for, I happened to inquire driving the DataSource, it casts it to BindingSource and discovered that a property "Current" which determines that KeyPar is selected internally from the link. So change the values to test and Bingo !
Dictionary<int,string> diccionario=new Dictionary<int,string>();
diccionario.Add(1,"Bella");
diccionario.Add(2,"Bestia");
this.comboList.DisplayMember = "Value";
this.comboList.ValueMember = "Key";
this.comboList.DataSource = new BindingSource(diccionario, null);
((BindingSource)this.comboList.DataSource).Position=0; // select init
///////Method's change ///////
((BindingSource)this.comboList.DataSource).Position =Convert.ToInt32(value);
I have simple comboBox with:
cb_listaUczniow.ValueMember = "Key";
cb_listaUczniow.DisplayMember = "Value";
And I have constructor for this Form (classID is not important yet):
MyForm(int classID, string selectedName)
{
cb_listaUczniow.ValueMember = "Key";
cb_listaUczniow.DisplayMember = "Value";
comboBox.DataSource = new BindingSource(makeList(classID), null);
}
makeList return Dictionary
and How i can select in comboBox item with "Value" (displayMember) where names selectedName?
for example (pseudo-Code):
MyForm(3, "Gall Anonim") -> comboBox.Item.Selected = comboBox.Item.where("Value" == "Gall Anonim");
How i can set it?
If I understand this correctly, you can simply set ComboBox's SelectedValue property to the corresponding value :
comboBox.SelectedValue = 3;
That will make "Gall Anonim" the selected item of the ComboBox.
I have a ComboBox with the following code:
private void comboBox1_TextChanged(object sender, EventArgs e)
{
using (var service = WebServiceHelper.GetCoreService())
{
string physicianXml = service.SearchPhysicians(SessionInfo.Current.ClientCode, SessionInfo.Current.MachineName,
SessionInfo.Current.Username, comboBox1.Text);
var physicians = PhysicianItemList.FromXml(physicianXml);
AutoCompleteStringCollection autoCompleteStringCollection = new AutoCompleteStringCollection();
foreach (var physician in physicians.Items)
{
autoCompleteStringCollection.Add(physician.LastName + ", " + physician.FirstName);
}
comboBox1.AutoCompleteCustomSource = autoCompleteStringCollection;
comboBox1.Select(comboBox1.Text.Length, 0);
}
}
Basically, a user types the first few characters of a physician's name and it should populate the auto-complete list with the top 100 matching records. It works great, but I need to associate it back to a key (either the PK from the table, or the Physician's NPI number). It seems the AutoCompleteStringCollection doesn't support keys. Can anyone suggest a way of doing this? There are approximately 7 million records in the table, so I don't want to prepopulate the ComboBox.
Thanks
When you build your AutoCompleteStringCollection, build a Dictionary<String, int> for the name, id pairs as well. Then use some event (textbox validation or user submit/save click) to lookup and set the id. You could store the dictionary on the textbox Tag.
Edit
For some reason I thought you were working with a textbox control. Forget about the AutoCompleteStringCollection and just build a Dictionary<String, int>. For the combobox set your autocompletesource to ListItems, set the combobox display name and value and set the datasource to the dictionary.
combobox.DisplayMember = "key";
combobox.ValueMember = "value";
combobox.AutocompleteSource = AutocompleteSource.ListItems;
combobox.DataSource = myDictionary;
However you should only populate the datasource and autocomplete once when the user enters n characters in the combobox, otherwise it gets buggy. I tried to use this for a dynamic autocomplete once (eg the list clears if the user clear the text and retypes), but I had to forget about the combobox and use a hybrid textbox listbox approach much like this user
It looks like your problem is that AutoCompleteStringComplete was made specifically for strings (hence, the name).
You may want to look into the parents (IList, ICollection, IEnumerable) and see if you can homebrew something templated toward a key/value struct.
Too late but maybe someone will use this code :
this.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
RNProveedor rnProveedor = new RNProveedor();
var listaProveedores = rnProveedor.Buscar();
Dictionary<int, String> dicTemp = new Dictionary<int, string>();
foreach (var entidad in listaProveedores)
{
dicTemp.Add(entidad.ProvNro, entidad.ProNombre);
}
this.DataSource = new BindingSource(dicTemp, null);
this.DisplayMember = "Value";
this.ValueMember = "Key";
And to select the value
public int GetValorDecimal()
{
KeyValuePair<int, string> objeto = (KeyValuePair<int, string>)this.SelectedItem;
return objeto.Key;
}
With this example you won't have any problem with duplicated strings as the examples above
When i try to bind a dictionary to a listbox I get an ArgumentException. Cannot bind to the new value member.
I use the following code.
Can any one tell me what is wrong. Because when i enter i row in the dictionary its working fine...
this.contactpersonenListBox = new Dictionary<int, string>();
lsContactpersonen.DataSource = new BindingSource(this.contactpersonenListBox, null);
lsContactpersonen.DisplayMember = "Value";
lsContactpersonen.ValueMember = "Key";
It doesn't make a ton of sense to bind an empty dictionary since the dictionary object doesn't report any changes, so adding an item to the dictionary after setting the data source won't show up in the ListBox.
But to get rid of the error, try setting it like this:
BindingSource b = new BindingSource();
b.DataSource = this.contactpersonenListBox;
lsContactpersonen.DisplayMember = "Value";
lsContactpersonen.ValueMember = "Key";
lsContactpersonen.DataSource = b;