WPF ListView sort on column clicked

I have a list for which items are added at runtime in a single / multi-column grid. Now I need this view to work. After there are items in the list view and they click on a column, it should sort it in that column.

Below is the code for the list

<ListView Name="lstValue" Margin="0,0,0,10"></ListView>

      

C # code where it populates the list view:

 case "Person":
                        dt = GetDataTable(GET_Person)
                        this.lstValue.View = gridview;
                        gridview.Columns.Add(new GridViewColumn { Header = "Number", 
                            DisplayMemberBinding = new Binding("Number") });
                        gridview.Columns.Add(new GridViewColumn { Header = "Name", 
                            DisplayMemberBinding = new Binding("Name") });
                        foreach(DataRow dr in dt.Rows)
                        {
                                                          this.lstValue.Items.Add(new ReportItem { Number = dr["Number"].ToString(),
                                Name = dr["Name"].ToString() });
                        }
                        break;

      

They should be able to sort by name or number.

+3


source to share


1 answer


This link is the MSDN way. The main thing is to handle the click on the column header of the gridview.

<ListView x:Name='lv' Height="150" HorizontalAlignment="Center" 
  VerticalAlignment="Center" 
  GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler"
 >

      

And in the code:



GridViewColumnHeader _lastHeaderClicked = null;
ListSortDirection _lastDirection = ListSortDirection.Ascending;

void GridViewColumnHeaderClickedHandler(object sender,RoutedEventArgs e)
{
  GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
  ListSortDirection direction;

  if (headerClicked != null)
  {
      if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
      {
          if (headerClicked != _lastHeaderClicked)
          {
             direction = ListSortDirection.Ascending;
          }
          else
          {
             if (_lastDirection == ListSortDirection.Ascending)
             {
               direction = ListSortDirection.Descending;
             }
             else
             {
                 direction = ListSortDirection.Ascending;
             }
          }

          string header = headerClicked.Column.Header as string;
          Sort(header, direction);

          _lastHeaderClicked = headerClicked;
          _lastDirection = direction;
       }
    }
  }

 private void Sort(string sortBy, ListSortDirection direction)
 {
  ICollectionView dataView =
    CollectionViewSource.GetDefaultView(lv.ItemsSource);

  dataView.SortDescriptions.Clear();
  SortDescription sd = new SortDescription(sortBy, direction);
  dataView.SortDescriptions.Add(sd);
  dataView.Refresh();

      

}

Basically it is. I have not included adding small directional glyphs to the column header to show the direction. If you want to see how to do this, you can refer to the full tutorial (see link above).

+6


source







All Articles