How to sort a column of a datagrid in VB.net

Goal: This example shows you how to programmatically sort a column in a datagridview.

Instructions: Open a new Windows Forms Application and add 1 button and a datagridview to the form. Run the application and enter some data in column1. Then press the button to sort. Replace all the automatically generated code in your project with the code below.

Code:

Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        ' Sort first column, ascending:
        DataGridView1.Sort(DataGridView1.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
 
    End Sub
 
End Class