2011-09-30 3 views
2

일부 데이터를 표시하기 위해 WPF 용 RADGridView를 사용하고 있습니다. DB에서 동적으로 가져 오므로 열 이름이나 각 셀에 포함 된 데이터 유형을 알지 못합니다. 열 머리글을 두 번 클릭 할 때 사용자가 각 열의 데이터를 정렬 할 수있게하려고합니다.Telerik RADGrid 및 정렬

어떤 이유로 그리드가 정렬되지 않습니다. 이것은 내가 지금까지 가지고있는 것이다.

private void SetEventHandlers() 
     { 
      if (_grid != null) 
      { 
       _grid.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick), true); 

      } 
     } 


private void OnCellDoubleClick(object sender, RoutedEventArgs e) 
     { 
      GridViewCellBase cell = e.OriginalSource as GridViewCellBase; 
      if (cell != null && cell is GridViewHeaderCell) 
      { 
       SetSorting(cell); 
      } 
     } 



private void SetSorting(GridViewCellBase cell) 
     { 
      GridViewColumn column = cell.Column; 
      SortingState nextState = GetNextSortingState(column.SortingState); 
      _grid.SortDescriptors.Clear(); 
      if (nextState == SortingState.None) 
      { 
       column.SortingState = SortingState.None; 
      } 
      else 
      { 
       _grid.SortDescriptors.Add(CreateColumnDescriptor(column, nextState)); 
       column.SortingState = nextState; 
      } 

     } 

편집 :

그것은 내 RadGrid 데이터가 ObservableCollection에에 바인더 제본 것보다 밝혀졌다
private ColumnSortDescriptor CreateColumnDescriptor(GridViewColumn column, SortingState sortingState) 
     { 
      ColumnSortDescriptor descriptor = new ColumnSortDescriptor(); 
      descriptor.Column = column; 
      if (sortingState == SortingState.Ascending) 
      { 
       descriptor.SortDirection = ListSortDirection.Ascending; 
      } 
      else 
      { 
       descriptor.SortDirection = ListSortDirection.Descending; 
      } 


      return descriptor; 
     } 
+1

명시 적으로 내장 정렬 기능을 해제 하시겠습니까? 기본적으로 열 머리글을 클릭하면 정렬이 수행됩니다 (따라서 두 번 클릭하면 정렬 위험이 있음). 또한 내장 정렬을 사용할 수없는 이유가 있습니까? 그러나 당신이하는 방식대로 그렇게해야합니다. 간단한 테스트를 할 수있었습니다. column.SortingState를 설정하는 행을 주석으로 처리해야한다고 생각합니다. 꼭 필요한 것은 아닙니다. 'CreateColumnDescriptor' 메소드를 게시 할 수 있습니까? –

+0

'CreateColumnDescriptor'코드를 게시했습니다. 기본 정렬을 사용하지 않도록 설정 한 이유는 상태를 정렬하고 데이터베이스에 저장하는 "보기"를 공유하기 때문입니다. 내가 이상한 소리가 난다. – Omar

+1

당신이 올린 코드가 그것을 시도 할 때 잘 작동하는 것 같다. 어떤 행동을 취하고 있습니까? 당신이 기대하는대로 일이 일어나고 있는지 확인하기 위해 여러 장소에 중단 점을 넣으려고 했습니까? 적절한 시간에'SetEventHandlers'를 호출한다고 가정합니다. 그리드가 null이면 이벤트를 첨부 할 수 없기 때문에 아무 것도 일어나지 않을 것입니다. –

답변

1

. 그리드 자체의 정렬 기능이 작동하지 않았습니다. ObservableCollection을 정렬하는 것이 해결책이었습니다. 나는 linq를 사용하여 ObservableCollection을 정렬했다.

관련 문제