2011-06-24 36 views
7

Tôi có một DataGrid WPF được điền dữ liệu từ DataSet. Tôi có CanUserSortColumns được đặt thành đúng.Giữ lại thứ tự sắp xếp do người dùng xác định trong WPF DataGrid

Có thể giữ lại phân loại mà người dùng đã chỉ định khi lưới được làm mới không? Tôi có nó duy trì mục mà đã được lựa chọn sử dụng

object selectedItem = dgInvoiceHeads.SelectedItem; 

trước khi làm mới diễn ra và sau đó đặt

dgInvoiceHeads.SelectedItem = selectedItem; 

sau khi làm mới diễn ra.

Nhưng tôi dường như không làm cho nó giữ nguyên loại được chỉ định.

Trả lời

3

Một trong những đồng nghiệp của tôi đã đưa ra điều này. Dường như nó hoạt động chính xác. Điều duy nhất là tôi nghĩ rằng các tiêu đề cột cần phải giống nhau trong DataGrid khi chúng ở trong DB.

string sortHeader; 
string prevSortHeader; 
SortDescription sd; 

private void dgInvoiceHeads_Sorting(object sender, DataGridSortingEventArgs e) { 
    sortHeader = e.Column.Header.ToString(); 

    if (sortHeader == prevSortHeader) { 
    sd = new SortDescription(sortHeader, ListSortDirection.Descending); 
    } 
    else { 
    sd = new SortDescription(sortHeader, ListSortDirection.Ascending); 
    } 
    prevSortHeader = sortHeader; 
} 

HTH

+2

Bạn có thể gặp vấn đề phải có tiêu đề giống như các thành viên lớp của bạn bằng cách sử dụng SortMemberPath trong cá thể cột chứ không phải thuộc tính Header. 'sortHeader = e.Column.SortMemberPath' – BrianVPS

3

Mã sau đây được lấy từ forum post và mã này hiển thị cách lấy mô tả sắp xếp và thông tin cột và khôi phục.

List<DataGridColumn> GetColumnInfo(DataGrid dg) { 
    List<DataGridColumn> columnInfos = new List<DataGridColumn>(); 
    foreach (var column in dg.Columns) { 
     columnInfos.Add(column); 
    } 
    return columnInfos; 
} 

List<SortDescription> GetSortInfo(DataGrid dg) { 
    List<SortDescription> sortInfos = new List<SortDescription>(); 
    foreach (var sortDescription in dg.Items.SortDescriptions) { 
     sortInfos.Add(sortDescription); 
    } 
    return sortInfos; 
} 

void SetColumnInfo(DataGrid dg, List<DataGridColumn> columnInfos) { 
    columnInfos.Sort((c1, c2) => { return c1.DisplayIndex - c2.DisplayIndex; }); 
    foreach (var columnInfo in columnInfos) { 
     var column = dg.Columns.FirstOrDefault(col => col.Header == columnInfo.Header); 
     if (column != null) { 
      column.SortDirection = columnInfo.SortDirection; 
      column.DisplayIndex = columnInfo.DisplayIndex; 
      column.Visibility = columnInfo.Visibility; 
     } 
    } 
} 

void SetSortInfo(DataGrid dg, List<SortDescription> sortInfos) { 
    dg.Items.SortDescriptions.Clear(); 
    foreach (var sortInfo in sortInfos) { 
     dg.Items.SortDescriptions.Add(sortInfo); 
    } 
} 
3

Bạn đã thử lấy bộ sưu tập cho tập dữ liệu chưa?

CollectionViewSource.GetDefaultView(yourDataSet).SortDescriptions 

Điều này sẽ cung cấp cho bạn một loạt các danh sách sắp xếp hiện tại. Sau đó, bạn có thể duy trì những điều này và vòng tiếp theo áp dụng chúng như sau

CollectionViewSource.GetDefaultView(yourDataSet).SortDescriptions.Add(...) 

Hy vọng điều đó sẽ hữu ích.

+2

Yep! điều này nên là giải pháp đúng .. và là giải pháp duy nhất. Và nếu tôi đúng, bạn thậm chí không phải "nhớ" phân loại trước đó hoặc nhóm: một khi chúng được khai báo trong collectionViewSource của bạn, tất cả các mục mới được thêm sẽ được sắp xếp tương ứng, như thể bạn chỉ làm một lực lượng tàn bạo '.Refresh() 'trên đối tượng' ICollectionView '... – Bruno

1
private void testGrid_Sorting(object sender, DataGridSortingEventArgs e) 
     { 

ListSortDirection direction = (e.Column.SortDirection != ListSortDirection.Ascending) ? 
           ListSortDirection.Ascending : ListSortDirection.Descending; 

// You will get the current direction in direction 

     } 

This is another solution 
Các vấn đề liên quan