How to Sort Two Unbound Columns in the DataGridView

Written by jaime avelar | 13/05/2017
How to Sort Two Unbound Columns in the DataGridView
Use Grid control in C#. (green binary background image by .shock from Fotolia.com)

Knowing how to sort two unbound columns in a DataGridView control can make your application more user-friendly. You can accomplish this task by using Microsoft Visual C#, an object-oriented computer programming language used to create Windows applications. A DataGridView control is used to display data in a tabular format and it's often needed to programmatically sort specific columns. Sorted columns help the programmer make the data more meaningful and easier to read.

Open Microsoft Visual C# Express and select "New Project..." from the left pane of your screen. Click "Visual C#" and under "Installed Templates" double-click "Windows Forms Application."

Click the "Toolbox" pane and double-click "DataGridView" to add a new Grid control. Double-click "Button" to add a new button to your Form.

Double-click "button1" to open the "Form1.cs" window.

Copy and paste the following code inside the "button1_Click" procedure to create a data table and a binding source:

Copy and paste the following code to add values to the data table:

Copy and paste the following code to create a data view and set the sort property:

Copy and paste the following code to set the properties of "Column1" and define the sort order to "None":

Copy and paste the following code to set the properties of "Column2" and define the sort order to "Ascending":

Copy and paste the following code to set the properties of "Column3" and define the sort order to "Ascending":

Press "F5" to run the program, then click "Button1" to fill the DataGridView control.

Things you need

  • Microsoft Visual C# Express

Show MoreHide

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.