I'm trying to mark one of the combobox items as selected.
So I am building my combobox like this:
var drop = new Dictionary<int, string>();
while (RegReader.Read())
{
drop.Add(Convert.ToInt32(RegReader["intRulesID"]), RegReader["txtName"].ToString());
}
RegRuleDrop.DataSource = new BindingSource(drop, null);
RegRuleDrop.DisplayMember = "Value";
RegRuleDrop.ValueMember = "Key";
Now, one of the items within the RegRuleDrop should be pre selected based on a value from a reader above this code.
Now, the problem is that I need to select value based on the actual ListItem VALUE and not TEXT.
So as an example
drop.Add(1, "Test");
drop.Add(2, "Test2");
drop.Add(3, "Test3");
I need to find the index using 1,2 or 3 not Test, Test2 or Test3
Any ideas?
When you have the DataSource set to a BindingSource the only action needed to select an item given a value belonging to the ValueMember property is
drop.Add(1, "Test1");
drop.Add(2, "Test2");
drop.Add(99, "Test99");
drop.Add(3, "Test3");
.....
RegRuleDrop.SelectedValue = 99
Related
This may be a very simple question but I realized I could not get it work.
I have a Winform combo box with datasource as a List<int>
combo.DataSource = intList;
What do I put for .DisplayMember and .ValueMember in order to simply have a list of integer values? Not setting them will display nothing.
I have worked with other List<myObj> in which DisplayMember and ValueMember are myObj's properties. How about simple data type like int, string?
When retrieving the selected item, one could simply cast (int)(combo.SelectedItem) or have to go through the property corresponding to ValueMember?
The problem does not occur because you have a list of integers, it probably occurs because you add items to the list after assigning it to the .DataSource property. List does not have a mechanism to notify its container when items are added to or removed from it.
Either add items to the list before assigning it to the .DataSource property, or use a wrapper like BindingSource as Krishnraj Rana suggested.
Here BindingSource comes into picture. You can use it like this.
BindingSource bSource = new BindingSource();
bSource.DataSource = new List<int> { 1, 2, 3 };
combo.DataSource = bSource;
Though you can set datasource of combobox directly with list. like this -
combo.DataSource = intList;
This also works perfectly fine.
You can add items from list using foreach like this.
foreach (var v in intList)
{
comboBox1.Items.Add(v.ToString());
}
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;
Upon selecting any item from the list, I want some other property to be passed than the one already being displayed.
First of all, you have to fill your LitBox items. You have two options here:
1 - By adding items
listBox.Items.Add(new KeyValuePair<Object, String>("Key", "Text"));
2 - By binding to a data source
listBox.DataSource = objectDataSource;
listBox.DisplayMember = "Name";
listBox.ValueMember = "ID";
Now, to get the selected item you can do the following:
KeyValuePair<Object, String> listBoxItem =
(KeyValuePair<Object, String>listBox.SelectedItem;
String text = listBoxItem.Value.ToString();
Object key = listBoxItem.Key;
Refer to :
ListBox Class
KeyValuePair Structure
Set listbox DataSource to your data source
Set listbox ValueMember to the "Id"
Set listbox DisplayMember to the value you want displayed, i.e. "Name".
I want to bind a OleDbDataReader to a Listbox . I have got reader with database values and i use following code for binding
List<String> cat_name = new List<string>();
while (reader.Read())
{
cat_name.Add(reader["cat_name"].ToString());
}
ProductMainCategory.DataSource = cat_name;
ProductMainCategory.DataBind();
i am getting category_id and cat_name from database. This code is setting only Listbox Text field with cat_name but i also want to fill dataValue property of Listbox. Please suggest me some good way to do this with best way .
You should be using List of some type such as KeyValuePair or Tuple or your own class to store both values. For example,
var categories = new List<KeyValuePair<string, string>>();
while (reader.Read())
{
categories.Add(new KeyValuePair<string, string>(reader["category_id "].ToString(), reader["cat_name"].ToString()));
}
ProductMainCategory.DataSource = Categories;
ProductMainCategory.DataValueField = "Key";
ProductMainCategory.DataTextField = "Value";
ProductMainCategory.DataBind();