C# 如何将项目插入到按字母顺序排列的列表框中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1680083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I insert an item to a listbox in its alphabetical place?
提问by Sakthivel
I develop a webpage in that I display a list box items which is fetched from a database. Dynamically I added some items into it. It adds to the end of the list box, so I want to sort the list box item after I add items. I tried Arraylist
for sorting, but it is not working.
我开发了一个网页,其中显示了一个从数据库中获取的列表框项目。我动态地添加了一些项目。它添加到列表框的末尾,所以我想在添加项目后对列表框项目进行排序。我尝试Arraylist
排序,但它不起作用。
回答by eugeneK
Do you mean by dynamically that you add items to list via Javascript or after post back of somekind using ASP.NET controls ?
您的意思是动态地通过 Javascript 或在使用 ASP.NET 控件回发后将项目添加到列表中吗?
If you add dynamically using Javascript then your answer would be sorting with jQuery and if after postback using ASP.NET then you should use List object located at System.Collections.Generic.
如果您使用 Javascript 动态添加,那么您的答案将是使用 jQuery 进行排序,如果在使用 ASP.NET 回发后,那么您应该使用位于 System.Collections.Generic 的 List 对象。
回答by Himadri
Try the following code:
试试下面的代码:
DataTable dt = new DataTable();
dt.Columns.Add("val", Type.GetType("System.Int32"));
DataRow dr;
for (int i = 1; i <= 31; i++)
{
dr = dt.NewRow();
dr[0] = i;
dt.Rows.Add(dr);
}
dt.AcceptChanges();
DataView dv = dt.DefaultView;
dv.Sort = "val desc";
ddlDay.DataTextField = "val";
ddlDay.DataValueField = "val";
ddlDay.DataSource = dv.ToTable();
ddlDay.DataBind();
If u r binding the listbox by setting datasource as DataTable populated with db data then when you want to add a new item instead of adding the item add the record to the datatable. Then create a dataview for that datatable, sort the data in the dataview as below:
如果您通过将数据源设置为填充了 db 数据的 DataTable 来绑定列表框,那么当您想要添加新项目而不是添加项目时,将记录添加到数据表中。然后为该数据表创建一个数据视图,对数据视图中的数据进行排序,如下所示:
DataView dv = dt.DefaultView;
dv.Sort = "val desc";
then set the datasource of listbox as dv.ToTable()
然后将列表框的数据源设置为 dv.ToTable()
回答by Zinx
You have to sort the datatable before assigning as a datasource to the list box.
您必须先对数据表进行排序,然后才能将其作为数据源分配给列表框。
DataTable.DefaultView.sort = "[Column Name]".
As you mentioned that you are using Add method to add the ListItems into List, Add method always appends the ListItems. You can add Items at desired index by using Insert method with index. List.Insert(ListItem, index)
正如您提到的,您正在使用 Add 方法将 ListItems 添加到 List 中,Add 方法始终附加 ListItems。您可以使用带索引的 Insert 方法在所需索引处添加项目。List.Insert(ListItem, index)
回答by The King
Try this code in PreRender
event of your ListBox
.
尝试在此代码PreRender
的事件ListBox
。
System.Collections.SortedList sorted = new SortedList();
foreach (ListItem ll in ListBox1.Items)
{
sorted.Add(ll.Text, ll.Value);
}
ListBox1.Items.Clear();
foreach (String key in sorted.Keys)
{
ListBox1.Items.Add(new ListItem(key, sorted[key].ToString()));
}
回答by Carl Heinrich Hancke
I was looking at a way of doing this without comparer classes, ArrayLists etc. So I thought I'd paste the method I had used for others:
我正在寻找一种没有比较器类、ArrayLists 等的方法。所以我想我会粘贴我用于其他人的方法:
Please note that my method uses LINQ to Objects, so it will only work in newer versions of the Framework.
请注意,我的方法使用 LINQ to Objects,因此它仅适用于较新版本的 Framework。
// get a LINQ-enabled list of the list items
List<ListItem> list = new List<ListItem>(ListBoxToSort.Items.Cast<ListItem>());
// use LINQ to Objects to order the items as required
list = list.OrderBy(li => li.Text).ToList<ListItem>();
// remove the unordered items from the listbox, so we don't get duplicates
ListBoxToSort.Items.Clear();
// now add back our sorted items
ListBoxToSort.Items.AddRange(list.ToArray<ListItem>());
回答by Ed M
Here is the vb code
这是vb代码
Private Sub SortList(ByVal lb As DropDownList)
' get a LINQ-enabled list of the list items
Dim list As New List(Of ListItem)(lb.Items.Cast(Of ListItem)())
' use LINQ to Objects to order the items as required
list = list.OrderBy(Function(li) li.Text).ToList()
' remove the unordered items from the listbox, so we don't get duplicates
lb.Items.Clear()
' now add back our sorted items
lb.Items.AddRange(list.ToArray())
End Sub
回答by Nancy Rocio
I know this is very similar to the response of The King, but this is how it worked for me.
我知道这与The King的回应非常相似,但这就是它对我有用的方式。
System.Collections.SortedList sorted = new System.Collections.SortedList();
foreach (ListItem ll in ListInQuestion.Items)
{
sorted.Add(ll.Value, ll.Text);//yes, first value, then text
}
ListInQuestion.Items.Clear();
foreach (String key in sorted.Keys)
{
ListInQuestion.Items.Add(new ListItem(sorted[key].ToString(), key));// <-- look here!
}
I hope this works for someone else.
我希望这对其他人有用。