このメソッドは、WPF の DataGrid コントロールで機能します。
1. 背景定義方法
///
/// 列ヘッダーのクリックをシミュレート
///
/// 列名
/// 降順または昇順
private void DataGridSort(string ColumnName, ListSortDirection DescOrAsce)
{
ICollectionView v = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource);
v.SortDescriptions.Clear();
v.SortDescriptions.Add(new SortDescription(ColumnName, DescOrAsce));
v.Refresh();
this.dataGrid1.ColumnFromDisplayIndex(0).SortDirection = DescOrAsce;
}
2. Call メソッド
//button イベントまたはその他のイベント
private void button1_Click(object sender, RoutedEventArgs e)
{ //Usage DataGridSort("column binding field name", ListSortDirection.Ascending); } 3. ソートされた結果セットを取得しますvar temp= control name.Items.Cast().Select(x => x as class name).ToList();//インターフェイス フィールドのソートされたコレクション
プログラムは次のようになります。