How to Sort Two Unbound Columns in the DataGridView

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
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.

Skill level:
Moderate

Other People Are Reading

Things you need

  • Microsoft Visual C# Express

Show MoreHide

Instructions

  1. 1

    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."

  2. 2

    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.

  3. 3

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

  4. 4

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

            BindingSource BS = new BindingSource();
    
            DataTable test Table = new DataTable();
    
            testTable.Columns.Add("Column1", typeof(int));
    
            testTable.Columns.Add("Column2", typeof(string));
    
            testTable.Columns.Add("Column3", typeof(string));
    
  5. 5

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

            testTable.Rows.Add(1, "Value1", "Test1");
    
            testTable.Rows.Add(2, "Value2", "Test2");
    
            testTable.Rows.Add(2, "Value2", "Test1");
    
            testTable.Rows.Add(3, "Value3", "Test3");
    
            testTable.Rows.Add(4, "Value4", "Test4");
    
            testTable.Rows.Add(4, "Value4", "Test3");
    
  6. 6

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

            DataView view = testTable.DefaultView;
    
            view.Sort = "Column2 ASC, Column3 ASC";
    
            BS.DataSource = view;
    
  7. 7

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

            DataGridViewTextBoxColumn textColumn0 = new DataGridViewTextBoxColumn();
    
            textColumn0.DataPropertyName = "Column1";
    
            dataGridView1.Columns.Add(textColumn0);
    
            textColumn0.SortMode = DataGridViewColumnSortMode.Programmatic;
    
            textColumn0.HeaderCell.SortGlyphDirection = SortOrder.None;
    
  8. 8

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

            DataGridViewTextBoxColumn textColumn1 = new DataGridViewTextBoxColumn();
    
            textColumn1.DataPropertyName = "Column2";
    
            dataGridView1.Columns.Add(textColumn1);
    
            textColumn1.SortMode = DataGridViewColumnSortMode.Programmatic;
    
            textColumn1.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
    
  9. 9

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

            DataGridViewTextBoxColumn textColumn2 = new DataGridViewTextBoxColumn();
    
            textColumn2.DataPropertyName = "Column3";
    
            dataGridView1.Columns.Add(textColumn2);
    
            textColumn2.SortMode = DataGridViewColumnSortMode.Programmatic;
    
            textColumn2.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
    
            dataGridView1.DataSource = BS;
    

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

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

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