To sort by multiple columns, use the DataView.Sort property to specify which columns you would like to sort by.
This is the C1List before the data sort:
Notice that on this form, you can choose which columns you would like to sort by and how you would like to sort them, either in ascending or descending order. After the user enters the column names and chooses the order, clicking the Sort button sorts the data.
Add the following code to your form, in this example the code was added to the Button1_Click event:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
If RadioButton1.Checked = True And RadioButton3.Checked = True Then Dim SortExpression = TextBox1.Text + " ASC" + "," + TextBox2.Text + " ASC" Me.CustomersBindingSource.Sort = SortExpression ElseIf RadioButton1.Checked = True And RadioButton4.Checked = True Then Dim SortExpression = TextBox1.Text + " ASC" + "," + TextBox2.Text + " DESC" Me.CustomersBindingSource.Sort = SortExpression ElseIf RadioButton2.Checked = True And RadioButton3.Checked = True Then Dim SortExpression = TextBox1.Text + " DESC" + "," + TextBox2.Text + " ASC" Me.CustomersBindingSource.Sort = SortExpression ElseIf RadioButton2.Checked = True And RadioButton4.Checked = True Then Dim SortExpression = TextBox1.Text + " DESC" + "," + TextBox2.Text + " DESC" Me.CustomersBindingSource.Sort = SortExpression End If |
To write code in C#
C# |
Copy Code
|
---|---|
if (radioButton1.Checked == true && radioButton3.Checked == true) { String SortExpression = textBox1.Text + " ASC" + "," + textBox2.Text + " ASC"; this.CustomersBindingSource.Sort = SortExpression; } else if (radioButton1.Checked == true && radioButton4.Checked == true) { String SortExpression = textBox1.Text + " ASC" + "," + textBox2.Text + " DESC"; this.CustomersBindingSource.Sort = SortExpression; } else if (radioButton2.Checked == true && radioButton3.Checked == true) { String SortExpression = textBox1.Text + " DESC" + "," + textBox2.Text + " ASC"; this.CustomersBindingSource.Sort = SortExpression; } else if (radioButton2.Checked == true && radioButton4.Checked == true) { String SortExpression = textBox1.Text + " DESC" + "," + textBox2.Text + " DESC"; this.CustomersBindingSource.Sort = SortExpression; } |
You can now enter column names into the textboxes and choose the sort order. Notice how choosing different orders on the columns changes the order of the data, in this case William Yahner appears before William Hunt because the LastName column is set to descending order.