WPF -- DataGrid の特定の列の並べ替えを制御するコードを実装します。

このメソッドは、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();//インターフェイス フィールドのソートされたコレクション




プログラムは次のようになります。
ここに画像の説明を挿入
ここに画像の説明を挿入
ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/weixin_41883890/article/details/128955877