ComponentOne List for WinForms
Customization / Sort by Multiple Columns
In This Topic
    Sort by Multiple Columns
    In This Topic

    List enables you to sort using multiple columns. To achieve this, you can modify the default view of the list using Sort property of the DataView class. Lets have a look at an example that can help you sort by multiple columns in List. In this example, you add a List control, two text boxes for entering column names, and two radio buttons for entering sort order as showcased in the following GIF.

    Sort by multiple columns

    The following example allows you to choose which columns you would like to sort by and how you would like to sort them, either in ascending or descending order. For this, you need to subscribe to the button click event and add the following code to the btnSort_Click event handler. This code creates a new sort expression based on the column names and the sort order entered by the user. Then, uses the Sort property to create a new data view for the List control. This example uses the sample created in the Quick Start topic.

    C#
    Copy Code
    private void btnSort_Click(object sender, EventArgs e)
    {
        string sortExpression = string.Empty;
    
        if (!string.IsNullOrEmpty(textBox1.Text) || !string.IsNullOrEmpty(textBox1.Text))
        {
            sortExpression = textBox1.Text;
    
            sortExpression += radioButton1.Checked ? " ASC" : " DESC";
        }
    
        if (!string.IsNullOrEmpty(textBox2.Text) || !string.IsNullOrEmpty(textBox2.Text))
        {
            if (!string.IsNullOrEmpty(sortExpression)) sortExpression += ", ";
    
            sortExpression += textBox2.Text;
    
            sortExpression += radioButton3.Checked ? " ASC" : " DESC";
        }
    
        productsBindingSource.Sort = sortExpression;
    }