FlexReport for .NET | ComponentOne
Reports in WPF / Sort Data
In This Topic
    Sort Data
    In This Topic

    Sorting is way to organize data in ascending or descending order based on certain criteria such as numeric, alphabetical, etc. Sorted data is generally easier to understand and maintain. In FlexReport, sorting is achieved by using the SortDefinitions collection of the DataSource class.

    Suppose you want to view the list of products with their names in ascending order. In this case, the list should be sorted by Name column. Perform the following steps to illustrate how to sort the names in the list of products in alphabetical order.

    1. In XAML view, add a Button and a FlexViewer control.
    2. Set the Name property of the FlexViewer control to  flexsortViewer. Also, set the Name of Button control to btnsort and Content to Sort Products by Name, as shown in the following code snippet:
      CS
      Copy Code
      <c1:C1FlexViewer Name="flexsortViewer" HorizontalAlignment="Left" Height="633" VerticalAlignment="Top" Width="958" Margin="0,28,0,0"/>
      <Button x:Name="btnsort" Content="Sort Products by Name" Click="btnsort_Click" HorizontalAlignment="Left" VerticalAlignment="Top" Width="191" Margin="300,5,0,0"/>
      
    3. Add the code in the code-behind file to load the report as done in the Data Binding section.
    4. Create a method with the name GetMyDataTable that returns a datatable for sorting as done in the Data Binding section
    5. Add the following code in the Click event of the btnsort control for sorting the data in the report. In the code, an object of SortDefnition class is created with the name sd to sort the report using the field [Name] in ascending order. The object sd is added to the SortDefinitions collection of flexReport.DataSource using the Add method. Finally, the Render method is called to refresh the report.
      CS
      Copy Code
      Boolean asc = true;
      if (asc == true)
      {
          SortDefinition sd = new SortDefinition("[Name]", SortDirection.Ascending);
          flexReport.DataSource.SortDefinitions.Add(sd);
          asc = false;
          flexReport.Render();
      }
      
    6. Run the application to view the report in the flexsortViewer control.
    7. Click the Sort Products by Name button to view sorting in the report.

      Sorting Data in FlexReport