Click here to Skip to main content
15,922,407 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 !!!
Posted

Use DataView to sort your data.

C#
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:
C#
dv.Sort = "EmpCode" + " ASC"; //for ascending
dv.Sort = "EmpCode" + " DESC"; //for descending


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

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

something like this ... :rose:
 
Share this answer
 

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