Click here to Skip to main content
15,029,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want gridview sorting and paging in asp.net
Posted
Comments
ssd_coolguy 21-Jun-12 13:02pm
   
hey dude let us know what u have tried..
and the main thing is... where is your question?

I am sure you must have not tried to look for it on web world before!

Here: MSDN: GridView Examples for ASP.NET 2.0: Paging and Sorting the GridView's Data[^]
   
this is very simple solution for get this

ASP.NET
<asp:gridview id="GridView1" allowsorting="true" allowpaging="true" pagesize="10" runat="server" />
   
v2
Comments
anshu77 17-Sep-12 2:51am
   
this is apply only throgh data source code but i want to do sorting through cod behind code
My Dear Friend Try this ??



C#
private string ConvertSortDirectionToSql(SortDirection sortDirection)
 {
     string newSortDirection = String.Empty;

     switch (sortDirection)
     {
         case SortDirection.Ascending:
             newSortDirection = "ASC";
             break;

         case SortDirection.Descending:
             newSortDirection = "DESC";
             break;
     }

     return newSortDirection;
 }

OK, Also Paste this below code in GridView1_Sorting Event.....

C#
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dataTable =GridView1.DataSource as DataTable;

    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

        GridView1.DataSource = dataView;
        GridView1.DataBind();
    }
}
   
v2
Comments
anshu77 17-Sep-12 3:43am
   
i have apply this code but i got a error like:
CS1061: 'ASP.mrhead_aspx' does not contain a definition for 'GridView1_Sorting1' and no extension method 'GridView1_Sorting1' accepting a first argument of type 'ASP.mrhead_aspx' could be found (are you missing a using directive or an assembly reference?)

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