Click here to Skip to main content
15,070,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Data in Datatable
First i want to sort it a/c to my first col of my datatable(Empcode) then i want to bind it with gridview

How should i do it???

Basically I m taking data from text file to put it into datatable then bind it to gridview
Help !!!

Use DataView to sort your data.

DataView dv = new DataView();
dv = dt.DefaultView; //cosidering dt as your datatable which contains data
dv.Sort = "SortExpression";

Here, SortExpression is your sort column(s).
You may also provide sort direction with the sort expression.

Something like this:
dv.Sort = "EmpCode" + " ASC"; //for ascending
dv.Sort = "EmpCode" + " DESC"; //for descending

Then bind the GridView to the DataView
Hope this helps!
If I am in your case, I would employ LINQ in this scenario.

dtobject.AsEnumerable().OrderBy(o => o["colname"].ToString());

something like this ... :rose:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900