hi freiends ,
i want to know How to sort datataable accordding alphabatic order ?
Thanks
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satish BhatRank #2,496 · 563 reputationPosted Jul 21, 2011, 1:56 AM EDT
There are two ways you can do this.
Using DataView
See the code below. I have created a DataView object by passing my DataTable as parameter, so my DataView will have all the data of the DataTable. Now, simply call the Sort method of the DataView and pass the sort expression. Your DataView object have sorted records now, You can either directly specify the Source of the Data controls object like GridView, DataList to bind the data or if you need to loop through its data you can use ForEach loop as below.
// Sorting DataTable DataView dataView = new DataView(dTable); dataView.Sort = " AutoID DESC, Name DESC"; foreach (DataRowView view in dataView) { Response.Write(view["Address"].ToString()); }Using DataTable.Select() method
Yes, you can sort all the rows using Select method too provided you have not specified any filter expression. If you will specify the filter expression, ofcourse your rows will be sorted but filter will also be applied. A small drawback of this way of sorting is that it will return array of DataRows as descibed earlier so if you are planning to bind it to the Data controls like GridView or DataList you will have for form a DataTable by looping through because directly binding arrays of rows to the Data controls will not give desired results.
Source: http://www.dotnetfunda.com/articles/article131.aspx
Blocked AccountRank #403 · 6.2K reputationPosted Jul 21, 2011, 1:59 AM EDT
You can also sort datatable by using DefaultView sort method
Priya LingeRank #785 · 2.9K reputationPosted Jul 21, 2011, 2:02 AM EDT
DataTable dt=new DataTable();
dt.DefaultView.Sort="FisrtName Desc";
Suppose you have dataset then,
DataSet ds=new DataSet();
ds.Tables[0].DefaultView.Sort="FirstName Desc";
Thanks.
Amit ChoudharyRank #145 · 15.8K reputationPosted Jul 21, 2011, 2:03 AM EDT
Try this
DataView dataView = new DataView(dTable);
dataView.Sort = "Name DESC";
dTable=dataaView.ToTable();
Hope this helped you.
Thanks.sagar Bhosale162 reputationPosted Jul 21, 2011, 4:30 AM EDT