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";
Related
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 creating combobox dynamically in winforms
ComboBox ddCntrl = new ComboBox();
ddCntrl.Width = 100;
ddCntrl.Name="dd_" + tpObj.RowColId;
ddCntrl.DropDownStyle = ComboBoxStyle.DropDownList;
Dictionary<int, string> DC = new Dictionary<int, string>();
DC[-1] = "N/A";
DC[0] = "Y";
DC[1] = "N";
ddCntrl.DataSource = new BindingSource(DC,null);
ddCntrl.DisplayMember = "Value";
ddCntrl.ValueMember = "Key";
ddCntrl.SelectedIndex = ddCntrl.Items.IndexOf("N");
TableLayoutPanel.Controls.Add(ddCntrl, 1, 1);
I tried couple of option to set the selected value nothing is working
I tried below options to set selected value
ddCntrl.SelectedValue ="N";
ddCntrl.SelectedIndex = ddCntrl.FindStringExact("N")
You will need to change some things. First, if you are using this code in the constructor, you will need to move it to Load or Shown event.
And set the index after add the comboBox to the panel. ddCntrl.FindStringExact("N") should works ok:
ComboBox ddCntrl = new ComboBox();
ddCntrl.Width = 100;
ddCntrl.Name = "dd_";
ddCntrl.DropDownStyle = ComboBoxStyle.DropDownList;
Dictionary<int, string> DC = new Dictionary<int, string>();
DC[-1] = "N/A";
DC[0] = "Y";
DC[1] = "N";
ddCntrl.DataSource = new BindingSource(DC, null);
ddCntrl.DisplayMember = "Value";
ddCntrl.ValueMember = "Key";
tableLayoutPanel.Controls.Add(ddCntrl, 1, 1);
ddCntrl.SelectedIndex = ddCntrl.FindStringExact("N");
Since you are binding to a Dictionary you should set selected item as follows:ddCntrl.SelectedItem = DC[1];
If you would like to set depending on display value (which i really do not suggest) you have to find it in DC and then set it to ddlCntrl
I currently have a dictionary which contains a key value and a list associated with that key.
I have read How to bind Dictionary to ListBox in winforms and when I try to implement that it just displays the key value.
What I am trying to do is have two separate listboxs. In box 1 you select the key value, when this happens box 2 displays the list. The current code is below:
var xmlDoc2 = new XmlDocument();
xmlDoc2.Load(textBox1.Text);
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
var node = xmlDoc2.SelectNodes("pdml/packet/proto[#name='ip']/#showname");
foreach (XmlAttribute attribute1 in node)
{
string ip = attribute1.Value;
var arr = ip.Split(); var src = arr[5]; var dst = arr[8];
List<string> l;
if (!dict.TryGetValue(src, out l))
{
dict[src] = l = new List<string>();
}
l.Add(dst);
listBoxSRC.DataSource = new BindingSource(dict, null);
listBoxSRC.DisplayMember = "Value";
listBoxSRC.ValueMember = "Key";
}
What this does so far is displays the key value in listBoxSRC, which is fine. What I need to do is display the list in listBoxDST.
I also looked at using ListView to rectify this issue but I couldn't figure out how it worked.
I know there should be a listBoxSRC_SelectedIndexChange somewhere but I keep getting 'dict doesn't appear in this context' errors.
Thanks
I jotted up something real quick with a pair of list boxes. Just make any form with a pair listboxes in it and wire up the event to try it yourself. By using the SelectedItem and casting it as the KeyValuePair that it is, you don't have to declare that dictionary outside of the method scope, as shown below.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.DataSource = new BindingSource(new Dictionary<string, List<string>>
{
{"Four-Legged Mammals", new List<string>{"Cats", "Dogs", "Pigs"}},
{"Two-Legged Mammals", new List<string>{"Humans", "Chimps", "Apes"}}
}, null);
listBox1.DisplayMember = "Value";
listBox1.ValueMember = "Key";
}
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
var keyValue = (KeyValuePair<string, List<String>>) listBox1.SelectedItem;
listBox2.DataSource = keyValue.Value;
}
else
{
listBox2.DataSource = null;
}
}
I am creating a new window called AMTI which is using comboboxes to display different selections for the user. The selection text could for example be "Analytical", in which case the value "E04" should be stored in appropriate column in the AMTI-table in the database. I read about using enums for databinding, but here a text and a numeric value are tied together, which can then be databound in the combobox. What is the easiest (or correct) approach for mapping a display text in a combobox with a value to be stored in the database?
You can bind ComboBox to any collection which elements expose public properties, including datatable, or, if you don't have a collection ready and need key-value objects, you can use Dictionary.
Dictionary<string, int> dict = new Dictionary<string, int>();
// fill the dictionary here
mycomboBox.DataSource = new BindingSource(dict, null);
mycomboBox.DisplayMember = "Key";
mycomboBox.ValueMember = "Value";
if(mycomboBox.SelectedIndex!=-1)
int currentlySelected = (int)mycomboBox.SelectedValue;
... or make your own class of objects for binding:
class NameValueHolder
{
public string Name{get;set;}
public int Value{get;set;}
public NameValueHolder(){}//so you can use it in linq
public NameValueHolder(string name, int value)
{
this.Name=name;
this.Value=value;
}
}
BindingList<NameValueHolder> list = new BindingList<NameValueHolder>();
list.Add(new NameValueHolder("object 1", 1);
list.Add(new NameValueHolder("object 2", 2);
list.Add(new NameValueHolder("object 3", 3);
mycomboBox.DataSource = new BindingSource(list, null);
mycomboBox.DisplayMember = "Name";
mycomboBox.ValueMember = "Value";
if(mycomboBox.SelectedIndex!=-1)
NameValueHolder currentlySelected = (NameValueHolder)mycomboBox.SelectedValue;
You can also bind ComboBox to Linq query result:
var qResult = from a in yourDataSource
where (/*some condition*/)
select new {Name = a.someName, Value = a.someValue};
mycomboBox.DataSource = qResult.ToList();
mycomboBox.DisplayMember = "Name";
mycomboBox.ValueMember = "Value";
Those are just some of the posibilities.
I have an empty listbox on .aspx page
lstbx_confiredLevel1List
I am generating two lists programatically
List<String> l1ListText = new List<string>(); //holds the text
List<String> l1ListValue = new List<string>();//holds the value linked to the text
I want to load lstbx_confiredLevel1List list box on .aspx page with above values and text. So i am doing following:
lstbx_confiredLevel1List.DataSource = l1ListText;
lstbx_confiredLevel1List.DataTextField = l1ListText.ToString();
lstbx_confiredLevel1List.DataValueField = l1ListValue.ToString();
lstbx_confiredLevel1List.DataBind();
but it does not load the lstbx_confiredLevel1List with l1ListText and l1ListValue.
Any ideas?
Why don't you use the same collection as DataSource? It just needs to have two properties for the key and the value. You could f.e. use a Dictionary<string, string>:
var entries = new Dictionary<string, string>();
// fill it here
lstbx_confiredLevel1List.DataSource = entries;
lstbx_confiredLevel1List.DataTextField = "Value";
lstbx_confiredLevel1List.DataValueField = "Key";
lstbx_confiredLevel1List.DataBind();
You can also use an anonymous type or a custom class.
Assuming that you have already these lists and you need to use them as DataSource. You could create a Dictionary on the fly:
Dictionary<string, string> dataSource = l1ListText
.Zip(l1ListValue, (lText, lValue) => new { lText, lValue })
.ToDictionary(x => x.lValue, x => x.lText);
lstbx_confiredLevel1List.DataSource = dataSource;
You'd better used a dictionnary:
Dictionary<string, string> list = new Dictionary<string, string>();
...
lstbx_confiredLevel1List.DataSource = list;
lstbx_confiredLevel1List.DataTextField = "Value";
lstbx_confiredLevel1List.DataValueField = "Key";
lstbx_confiredLevel1List.DataBind();
Unfortunately the DataTextField and DataValueField are not used like that. They are the text representation of the fields they're supposed to show of the current item that's being databound in the DataSource.
If you had an object that held both text and value, you'd make a list of it and set that to datasource like this:
public class MyObject {
public string text;
public string value;
public MyObject(string text, string value) {
this.text = text;
this.value = value;
}
}
public class MyClass {
List<MyObject> objects;
public void OnLoad(object sender, EventArgs e) {
objects = new List<MyObjcet>();
//add objects
lstbx.DataSource = objects;
lstbx.DataTextField = "text";
lstbx.DataValueField = "value";
lstbx.DataBind();
}
}