C# combobox select new item - c#

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);

Related

How to add data from one listbox to another listbox?

The following data are displayed in listbox1. I want to pass items from listbox2 to listbox1. The result should be look as shown below (lb1).How can this be achieved?
Many thanks for your help!
var itm = listBox2.Items[0].ToString();
listBox1.Items.Add(itm);
enter image description here
Use AddRange Method. Try like:
listBox1.Items.AddRange(listBox2.Items);
You can use AddRange
listBox1.Items.AddRange(listBox2.Items);
Or if you want an exact copy of what is in the other listbox without your old items.
listBox1.Items = listBox2.Items;

Sorting a ListView by a default column

I have a xaml listview with an item source of an observable collection that has a payload of a 'Person' object. Is there a way to get the listview to DEFAULT sort by a column if an item is added or removed from the collection? So, say the Person object has a property of 'Age'. When an item is added or removed, how to I get the listview to automatically sort by age? I've been looking for a solution and I do not see one.
Just on addItem, and removeItem functions, add the following at the end :
SortDescription mySort = new SortDescription(Age, ListSortDirection.Ascending);
this.ListView1.Items.SortDescriptions.Clear();
this.ListView1.Items.SortDescriptions.Add(mySort);
This will add a sorting on "Age" Property.

Sorting a ListBox ListItems with LINQ?

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.

Set selected item in a databound list box

I've got a wpf listbox that is bound to a datatable. At some times, I want to programmaticly change the selection of the listbox. I know the text of the item I want to select. But it doesn't work to set the listbox1.SelectedItem to the text I want because the type of the SelectedItem is System.Data.DataRowView.
If I have the text I want to select and the DataRow that I want to select, what is the easiest way select the associated row in the list box?
Search through your DataSet and find the appropriate DataRow. Then set SelectedItem to that DataRow.
If you know the text, then it would be:
ListBox1.SelectedValue = ListBox1.Items.FindByText("Two").Value;
You can also use the SelectedIndex property to set the selected value by 0-based index.
The ListBox control (both in Forms and WebControls) has a SelectedValue property that:
"Gets the value of the selected item in the list control, or selects the item in the list control that contains the specified value."
You could use this to select the item based on it's value, typically a unique key.
More info from MSDN:
System.Windows.Forms.ListControl.SelectedValue
System.Web.UI.WebControls.ListControl.SelectedValue

Dropdowns filled with same list item

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...

Categories

Resources