If we have a large amount of data in a datagrid, it will be useful for us if we having some kind of sorting mechanism in it so that we can look at whatever kind of record we want. We are going to implement the sorting procedure of a ASP.NET datagrid.
Let me take u step-by-step through the sorting process.
-
First in the HTML declaration of the data grid, add the attribute called AllowSorting="True". Now you will get a hyperlink on all the column names.
-
Go to the declaration of individual data bound columns and add the attribute called SortExpression and its value for the particular data field for that column. Now you will get a link for that particular column header.
-
Now go to the events of the datagrid and create an event for the SortCommand.
-
Go again to the datagrid declaration and add a attribute called OnSortCommand=true.
-
Now in the code-behind, we write a code for the sort event in the datagrid.
Public
void DataGrid1_SortCommand (object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
//
//
}
-
Now get the column name to be sorted using e.SortExpression.
-
When you have the column name, you can sort the DataView using the column name. E.g
When u get the dataset to be bounded to the datagrid. Create a DataView from it and use the Sort method of it.DataSet dsShippers;
new DataView();
dsShippers=getNorthWindShippersData();
DataView dvShippers=
dvShippers=dsShippers.Tables[0].DefaultView;
if(SortWay%2==0)
dvShippers.Sort=sortfield+" "+"ASC";
else
dvShippers.Sort=sortfield+" "+"DESC";
SortWay++;
dgShippers.DataSource=dvShippers;
dgShippers.DataBind();
-
Now go back to your HTML code, add an attribute for datagrid called onSortCommand and give the value of the function name of the sort command in the code behind.
<
DataGrid id="dgShippers" .. .. .. onSortCommand="DataGrid1_SortCommand" AllowSorting="true">
Now you have a two-way sorting datagrid. Hope this provides you with some knowledge about datagrid sorting. For the datagrid paging you can use the in built paging mechanism of the datagrid, which is quite satisfactory.

rengarajan govindarajan srinivasanPosted Jun 20, 2006, 1:05 AM
Hi krishnan, I have some queries to be clarified as early as possible. Iam integrating my web application with MSProject 2003. I have used a com dll Microsoft.Office.Interop.MsProject.dll.When i referred this comdll to my application it should be converted into .net compatible format so that .net can communicate with it. Thats why while running the office 2003PIA(primary interop assemblies).exe ,i can make the com dll to be placed inside the path (C:\windows\assembly) ie (Inside GAC). In my web application i have used Forms authentication.When i run the application and particularly when it creates instance for COMclass,It shows Access denied exception. But to temperovarily avoid this i have disabled the anonymous access in IIS and in web.config i give <identity impersonate="true"/>.I want to know why this is happening?Can't i use the comdll using forms authentication .Also i have given Asp.Net grant access rights to the file. Can u suggest me a solution for this. Regards G.S.Rengarajan.
ratnesh singhPosted Feb 3, 2006, 2:27 AM
Hi i was jst going through ur article .. datagrid two way sorting . i am unable to get SortWay u have used in ur coding! how to find which header link is clicked So tht i can sort the data accordingly pl reply [email protected] rgrds Ratnesh