Sort dataGridView columns in C# ? (Windows Form)

AXheladini picture AXheladini · Apr 30, 2009 · Viewed 175.6k times · Source

I have a datagridview that i bind from an sql table, in that dv i have those attributes: Id, Name and Price. When i set the SortMode of the Name Columns to Automatic and i click on the header of this column i can sort this dv based on the first letter of the Name, this way i can order products based on their first letters ( Acumulator, Boat, CocaCola, Engine etc).

Is there a way this thing to happen without clicking the header of the column Name. I am looking some code that will do this job when the form will load.

Answer

BFree picture BFree · Apr 30, 2009

There's a method on the DataGridView called "Sort":

this.dataGridView1.Sort(this.dataGridView1.Columns["Name"], ListSortDirection.Ascending);

This will programmatically sort your datagridview.