Sorting a ListBox ListItems with LINQ? - c#

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.

Related

Can't manually add item to listbox that is populated iterating through a list

In a C# WinForms project I'm querying a database to get a list of values to populate a listbox with. The query populates a List and then I iterate through that to add the list items to the listbox.
lsNewValuesList = dbGetNewValueInfo.GetNewValuesDgvData(strNewValuesQuery);
foreach (string strItem in lsNewValuesList)
{
lsBxNewValues.Items.Add(strItem);
}
After that's done, I need to manually add an item to the top of the list, which I'm doing via, lsBoxNewValues.Items.Insert(0, "DELETE");. But when I run it I get the message,
Items collection cannot be modified when the DataSource property is set.
Here is a screenshot of the error (to clarify some questions):
Looking into that I'm understanding that error arises when the listbox is populated with a datasource, but I'm just populating it from a string list. Is that, technically, a datasource then?
How do I accomplish what I'm trying to do?
[UPDATE]
Okay, I've done some fiddling around, albeit without resolving my issue, and I've tried the following:
lsNewValuesList = dbGetNewValueInfo.GetNewValuesDgvData(strNewValuesQuery);
lsNewValuesList.Insert(0, "DELETE");
lsBxNewValues.DataSource = lsNewValuesList;
//foreach (string strItem in lsNewValuesList)
//{
// lsBxNewValues.Items.Add(strItem);
//}
Instead of inserting "DELETE" in the ListBox (which is what I was originally trying and was causing the error), I inserted it at index 0 of the List, the datasource of the ListBox, and then set the ListBox's datasource. "DELETE" is showing up in the ListBox, but it's getting alphabetized along with the rest of the items. I'm not doing any sorting of the list or the ListBox - I'm using ORDER BY in the database query, however. CyberZeus suggested I use Refresh() on the ListBox, but that didn't have any effect.
Yes, a list of strings is a datasource.
You have to add the data retrieved from the database to the datasource of the ListBox. You may need to refresh the ListBox.

How can i find item in wpf listbox having grouped list bind to it?

My listbox is having a grouped list so basically I want to find listbox group item index with item value. Listbox is having item source bind to it and is having DisplayMemberPath and SelectedValuePath set from code behind.
What I have tried yet are as follow:-
int index = istboxName.Items.IndexOf(ListBindToItemSource.particularParameterValue);
Give index=-1 always.
Another solution I tried is:
int index = ListboxName.Items.Groups.IndexOf(ListBindToItemSource.particularParameterValue);
Same result index=-1 always.
You should never need to access the items that way, instead access the item in your bound source and manipulate that. If you want to change anything in the view, like e.g. a Background, bind it on your item and change it at the source.

Keep ListBox sorted, but with a particular entry always pinned to the top

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.

Remove or add items in DataGridComboboxColumn on row add or delete

Here's my problem: I need to make a DataGrid with dynamic comboboxes using the WPF. If the value of a combobox is already used in the previous rows, the next ones, that will be added by the user, shouldn't contain the item already used.
In this image, the ITEM A shouldn't apear on the combobox of the second line.
I don't have ideia how to accomplish this, can anyone show me a light?
OBS: The DataGrid ItemsSource is binded to an ObservableCollection, and the DataGridComboBoxColumn ItemsSource is a List.
Thanks !!!
The ItemsSource of the combo doesn't have to be bound to an ObservableCollection, but it can help depending on exactly how you solve this.
When that cell goes in to edit mode the property the ItemsSource is bound to gets hit - so you can return a new list of items each time the getter is hit. Here is a very basic example to give you an idea:
public List<string> MyItemsSource
{
get
{
var myNewList = MyMasterList.ToList(); //create a (reference) copy of the master list (the items are not copied though, they remain the same in both lists)
if (PropertyA != null)
myNewList.Remove(PropertyA);
return myNewList;
}
}
So what you are creating and returning is a filtered version of your master list of all possible items. LINQ will be of great help to you here.
Alternatively you could keep just one static copy of the master list as an ObservableCollection, and simply remove items from that static copy as they get selected (and add them back in as they get unselected). Which option you choose will depend on how many times the list can be modified due to items being selected and how complicated it is to generate the list. I've used the dynamically generated list many times in the past, it's an option that works well in most cases.

C# combobox select new item

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

Categories

Resources