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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
sagar BhosalePosted Jul 21, 2011, 8:30 AM
Amit ChoudharyPosted Jul 21, 2011, 6:03 AM
Try this
DataView dataView = new DataView(dTable);
dataView.Sort = "Name DESC";
dTable=dataaView.ToTable();
Hope this helped you.
Thanks.Priya LingePosted Jul 21, 2011, 6:02 AM
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.
Blocked AccountPosted Jul 21, 2011, 5:59 AM
You can also sort datatable by using DefaultView sort method
Satish BhatPosted Jul 21, 2011, 5:56 AM
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.
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