ComponentOne List for WinForms
In This Topic
    Sort
    In This Topic

    List provides you complete control on how data can be sorted in ascending or descending order for easier data analysis. This topic discusses about sorting in List and other sort related operations.

    By default, List allows you to sort a single column in ascending and descending order by clicking its column header. The following sections take you through the ways to perform various operations related to sorting.

    Sort through Code

    To sort a specific column, use Sort method of the C1List class. This method lets you specify the column and sort direction.

    The following image shows the List control after sorting the "CategoryID" field in descending order.

     

     Use the following code to apply sorting on a specific column. In this example, we have applied sorting to the "CategoryID" field in descending order.

    C#
    Copy Code
    //apply sort to the CategoryID Column
    c1List1.Sort("CategoryID", C1.Win.List.SortDirEnum.DESC);
    

    Clear Sort

    By default, List does not provide in-built functionality to undo a sort operation. However, there might be a scenario where you would want to undo a sort operation and return to the default view of List. To do so, you can set Sort property of the DataView class to an empty string (" "). Please note that clearing sort using the Sort property is possible only if the List control is bound to a dataset.

    The following code demonstrates how you can return the data to the default view by using the Sort property. The following examples uses the sample created in the Quick Start topic.

    C#
    Copy Code
    this.productsBindingSource.Sort = " ";
    

    Custom Sort

    List provids various options for sorting the entire list or specified columns in a list. However, there might be a scenario where you would want to apply your own custom sorting. To do so, first you need to disable sorting in the List by setting AllowSort property of the C1List class to false.

    To implement custom sorting in the List control, follow these steps.

    1. Add the following code to make the column headers act like buttons and set the sort order to None.
      C#
      Copy Code
                  
      foreach (C1DisplayColumn dc in c1List1.Splits[0].DisplayColumns)
      {
          // make the column headers act like buttons
          dc.ButtonHeader = true;
          // default sort order
          dc.DataColumn.SortIndicator = SortDirEnum.None;
      }
      
    2. Subscribe to ColHeadClick event of the C1List class. Add the following code to the event handler for c1List_HeadClick to apply custom sorting on the list. In the following code, we create a new sort order for the column fields and then apply sorting after checking for the existing sort criteria on the column fields. Here, the Sort property is used to modify the default view of the list.
      C#
      Copy Code
      private void c1List1_HeadClick(object sender, ColEventArgs e)
      {
          // get the display column that was clicked
          {
              C1DisplayColumn dc = this.c1List1.Splits[0].DisplayColumns[e.ColIndex];
              // new sort order
              SortDirEnum newsort = (SortDirEnum)dc.DataColumn.SortIndicator;
              switch (newsort)
              {
                  case SortDirEnum.None:
                      dc.DataColumn.SortIndicator = SortDirEnum.ASC; 
                      break;
      
                  case SortDirEnum.ASC:
                      dc.DataColumn.SortIndicator = SortDirEnum.DESC;
      
                      break;
      
                  case SortDirEnum.DESC:
                      dc.DataColumn.SortIndicator = SortDirEnum.None;
                      break;
              }
      
              //find if data field already has a sort criteria in existing sortCondition
              int posOfFieldName = sortCondition.IndexOf(dc.DataColumn.DataField);
      
              if (posOfFieldName == -1)
              {
                  //if data field is not found and doesn't have an existing sort criteria
                  if (sortCondition == string.Empty)
                  {
                      sortCondition = dc.DataColumn.DataField + " ";
                  }
                  else
                  {
                      sortCondition += "," + dc.DataColumn.DataField + " ";
                  }
                  sortCondition += dc.DataColumn.SortIndicator.Equals(SortDirEnum.DESC) ? "DESC " : "ASC ";
              }
              else
              {
                  int startPosOfFirstSpace = sortCondition.IndexOf(" ", posOfFieldName + 1);
                  int startPosOfSecondSpace = sortCondition.IndexOf(" ", startPosOfFirstSpace + 1);
                  sortCondition = sortCondition.Remove(posOfFieldName, startPosOfSecondSpace - posOfFieldName + 1);
                  if (dc.DataColumn.SortIndicator != SortDirEnum.None)
                  {
                      sortCondition = sortCondition.Insert(posOfFieldName, dc.DataColumn.DataField.ToString() + " " + (dc.DataColumn.SortIndicator.Equals(SortDirEnum.DESC) ? "DESC " : "ASC "));
                  }
                  if (posOfFieldName > 0 && sortCondition.Length == posOfFieldName)
                  {
                      //if there is no field ahead then remove the previous comma also
                      sortCondition = sortCondition.Remove(posOfFieldName - 1, 1);
      
                  }
                  else if (sortCondition != string.Empty && sortCondition.Substring(posOfFieldName, 1) == ",")
                  {
                      if (posOfFieldName > 0)
                      {
                          sortCondition = sortCondition.Remove(posOfFieldName, 1);
                      }
                      else if (posOfFieldName == 0)
                      {
                          sortCondition = sortCondition.Remove(posOfFieldName, 1);
                      }
                  }
              }
             // sort the list
              dt.DefaultView.Sort = sortCondition;
              this.Text = "C1List Sort Condition: " + (sortCondition != "" ? sortCondition : "None");
          }
      }
      

    Sort Indicator

    By default, the List control displays upward and downward triangles as sort indicators. However, you can easily customize these sort indicators while implementing custom sorting.

    In the following image, we have used customized sort indicators in the List control.

     

    To customize sort indicators, you can use the ForegroundImage property of the Style class as shown in the following code snippet. In this example, we have customized the sort indicator of the first column in the split.

    C#
    Copy Code
     
    C1DisplayColumn dc = c1List1.Splits[0].DisplayColumns[0];
    dc.HeadingStyle.ForegroundImage = Image.FromFile("SortUp.bmp");