[英]WPF 4 Datagrid with ComboBox, CollectionViewSource and Entity Framework
[英]WPF ComboBox with entity framework orderBy
我像這樣在我的項目中填充了ComboBox
CB_City.ItemsSource = c.Cities;
CB_City.DisplayMemberPath = "city1";
CB_City.SelectedValuePath = "city_id";
CB_City.SelectedValue = 517;
我使用實體框架和c#,如何按升序排序結果?
仍然不能(我正在嘗試很多)我附上完整的代碼
using (MorEntities1 c = new MorEntities1())
{
CB_City.ItemsSource = c.Cities;
CB_City.DisplayMemberPath = "city1";
CB_City.SelectedValuePath = "city_id";
CB_City.SelectedValue = 517;
}
您可以使用: CB_City.ItemsSource = c.Cities.OrderBy(c=>c.Text)
或CB_City.ItemsSource = c.Cities.OrderBy(c=>c.Text)
另一種選擇是使用帶有SortDescription
的CollectionViewSource
:
var myViewSource = new CollectionViewSource { Source = c.Cities.ToList() };
myViewSource.SortDescriptions.Add(
new SortDescription("YOUR_PROPERTY", ListSortDirection.Ascending)
);
CB_City.ItemsSource = myViewSource.View;
在@Ross的帖子之后,您還可以通過以下方式獲取CollectionViewSource
:
var view = CollectionViewSource.GetDefaultView(CB_City.ItemsSource);
view.SortDescriptions.Add(new SortDescription("city1",
ListSortDirection.Ascending));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.