Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort dataGridView columns in C# ? (Windows Form)

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.

like image 730
AXheladini Avatar asked Apr 30 '09 12:04

AXheladini


People also ask

How to enable sorting in DataGridView c#?

You can then change the numerical cell values into image display values in a handler for the DataGridView. CellFormatting event. In this case, setting the SortMode property to Automatic will enable your users to sort the column.


2 Answers

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

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

This will programmatically sort your datagridview.

like image 110
BFree Avatar answered Sep 23 '22 00:09

BFree


dataGridView1.Sort(dataGridView1.Columns[0],ListSortDirection.Ascending); 
like image 39
2 revs, 2 users 67% Avatar answered Sep 23 '22 00:09

2 revs, 2 users 67%