I have a Gridview in which i have two templatefields of dropdownlist. I bound them on runtime with same list item.
li = new listitem ("1","1");
dl1.items.add(li);
dl2.items.add(li);
li = new listitem ("2","2");
dl1.items.add(li);
dl2.items.add(li);
li = new listitem ("3","3");
dl1.items.add(li);
dl2.items.add(li);
dl1.selectedvalue = "2";
dl2.selectedvalue = "3";
After executing above, dl1 & dl2 both show me "3" as selected value. Why?
I know the work around of using 2 different listitems while binding but i wanna know why the above happens?
Looking at just the last part of the code: you've got a single list item, and it's appearing in two different lists. But it's still one object. How would you expect one object to have two different values for a single property (SelectedValue)?
The ListItem class has a property "Selected" which marks if the item is selected. I haven't checked the DDL SelectedValue property to see what it does, but my guess is that the ListItem.Selected property is being set to true, and since you are using the same object in both drop-down lists, it is being marked as 'selected' in both.
I'm sure if this was a multi-select list, both "2" and "3" would be marked as 'selected'.
You have to instantiate each list item for each drop down list.
ListItem li1 = new ListItem("1","1");
dl1.items.add(li1);
ListItem li2 = new ListItem("1", "1");
dl2.items.add(li2);
Edit:
Jon described what I want to mean. You have only one object that has a value. So don't expect different values for each drop down list.
When you set dl1 to "3" then both of them will get the same value because both drop down lists reference to same object!
the listitem is being shared among the two drop downs. when you set the selected value for one of the drop downs it sets the list item as being selected. since the listitem is being shared it's selected in both drop downs
I would think it would be a ref vs value problem. I am sure both d1 and d2 would be pointing to the same spot in memory if the are added from the same list item...
Related
Im trying to populate 5 different comboboxes with the same datasource. The datasource is from a class list with values like this (the values that arent displaymember im using to populate text-boxes adjacent to the combobox):
StandardList.Add(new Standard() { ID = 1, number = 91632, credits = 4,
uniEntrance = "Y", assessmentType = "Ext", standardType = "AS" });
This is how I have populated each combobox:
cboNumber1.BindingContext = new BindingContext();
cboNumber1.ValueMember = "ID";
cboNumber1.DisplayMember = "number";
cboNumber1.DataSource = StandardList;
cboNumber1.Refresh();
When I select an item from one particular combobox, I want to remove that same item from the remaining comboboxes, and then re add it to each combobox if that item is deselcted in the focussed combobox. I have not figured out how to 're add' yet, however the 'removing' part works for every item except when I try to select the LAST item in the combobox using
StandardList.RemoveAt(cboNumber1. SelectedIndex);
it comes up with "ArgumentOutOfRangeException". My question is how do I get around this? Also if I select a particular item in the focussed combobox, and it removes that same item from each remaining combobox, how would I re-add that particular item back, at the same position, to each combobox if I deselected that particular item in the focussed combobox?
This is my first post so sorry for any bad practice. Also please bear in mind im still a highschool student so my c# knowledge is not very broad and my coding practices may be inefficient/bad. I would appreciate any answers/solutions in simple terms even if it means inefficiencies. Thanks.
On Selection change of the 1st combo box try to set the filtered DataSource of other combo boxes by using "Where" of list class.
like:
StandardList.Where(item => item.ID != cboNumber1.SelectedItem.Value);
This will return a filtered list, assign this list to other combo boxes.
This will work for you. I guess..
I have a ListBox filled with Countries (which I take from the Active Directory). I want the list to be sorted, but also I want one entry "All" to be at the very top.
How can I do this?
If you are binding the data in code behind you can insert a Listitem at index 0.
ListItem myItem=new ListItem("ALL","value");
myListbox.Items.Insert(0, myItem);
I would sort the list items first before binding to you ListBox. There are several options for doing this depending on what your data source is i.e. DataTable, List, Dictionary etc. To insert an item use code below.
lstCountries.Items.Insert(0, new ListItem("All", "0"));
After you load the data (i.e Countries), add the ListItem as follows:
myListbox.Items.Add(new ListItem() { Text = "All", Value = "" });
myListbox.SelectedIndex = 0; //This line will selected the first item on your ListBox.
Here, you might consider which action take if the ListBox with text "All" is selected.
I'm trying to sort the items in a ListBox. However, upon doing so the Item Value gets set to the Item Text. Any help would be appreciated.
lbxCustomers.DataSource = lbxCustomers.Items.Cast<ListItem>().Reverse().ToList();
lbxCustomers.DataBind();
May be first you should store the list in generic collection and then sort it. Something like this:
List<ListItem> list = new List<ListItem>(lbxCustomers.Items.Cast<ListItem>());
list = list.OrderBy(li => li.Text).ToList<ListItem>();
lbxCustomers.Items.Clear();
lbxCustomers.Items.AddRange(list.ToArray<ListItem>());
Try resetting the DisplayMember and ValueMember properties of the Listbox after setting the DataSource. A lot of times if a control is already bound and you set the DataSource again, then it drops the DisplayMember/ValueMember properties.
Try sorting the list separately.
Point the Text value and Data value in the next step.
Databind the control.
Is it possible to set the selected item of a combobox to be an object that is not in its dropdown list?
If yes, then what must one do?
Use the Text property:
comboBox.Text = "I'm not in the list!";
If you want to add the item to the list, use the Items collection:
comboBox.Items.Add("I was added to the list!");
Is there a reason you cannot add it to the list after the object was generated? Is the object generated asynchronously?
myComboBox.Items.Add(newItem);
I am using C# with a Windows Application Form. In this I have a combobox. What is the code to add the dropdown selections? From my googling so far I presume I need to setup an arraylist for the details?
To add Items to the ComboBox you have two options:
Either add them to the Items collection:
comboBox1.Items.Add("abc");
comboBox1.Items.Add("def");
Or use data binding:
comboBox1.DataSource = myList;
or with an array:
comboBox1.DataSource = myArray;
For the first variant you can only use strings as items, while with data binding you can bind a collection of more complex objects. You can then specify what properties are displayed:
comboBox1.DisplayMember = "Name";
and what are treated as value:
comboBox1.ValueMember = "ID";
You can access the original object that is selected later with
comboBox1.SelectedItem
or the value with
comboBox1.SelectedValue
The value is the property you specified with ValueMember.
You can use ComboBox1.Items.Add("Item") to add items 1 at a time, or ComboBox1.Items.AddRange(MyArray) to add a whole list of items at once. Each item that you add can be a string, in which case it is displayed directly in the dropdown list, or it can be an object, in which case the DisplayMember property of the combo box is used to determine which of the objects properties will appear in teh dropdown list.