Click here to Skip to main content
15,850,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am shwetha

I have a gridview with below features,
1)Edit
2)Update
3)Cancel
4)paging
5)Multiple column delete with check box option.

I have also sorted the gridview data using sort Expression.I have just 3 records to be displayed per page.When i click for second page, the sorted data is lossed.How to retain the sorted data for next page also, even after page_indexChanging event? please help me...

My gridview code:
VB
<asp:GridView  DataKeyNames="Emp_ID" ID="GridView1"
            runat="server" BackColor="#CEF2CE"
                            AutoGenerateColumns="False" AllowPaging="True" BorderColor="Black"
                            BorderStyle="Groove"
                            PageSize="3" onpageindexchanging="GridView1_PageIndexChanging"
                            onrowcancelingedit="GridView1_RowCancelingEdit"
                            onrowdatabound="GridView1_RowDataBound" onrowediting="GridView1_RowEditing"
                            onrowupdating="GridView1_RowUpdating" AllowSorting="True"
                        onsorting="GridView1_Sorting"
                        onpageindexchanged="GridView1_PageIndexChanged">



XML
<PagerSettings PageButtonCount="3" />
                       <Columns>
                           <asp:TemplateField HeaderText="Emp_ID" SortExpression="Emp_ID"
                           ControlStyle-Font-Underline="false">
                           <ItemTemplate>
                           <asp:Label ID="Label5" runat="server" Text='<%# Eval("Emp_ID") %>'>
                           </asp:Label>
                           </ItemTemplate>
                           <ControlStyle Font-Underline="False"></ControlStyle>
                           </asp:TemplateField>





aspx.cs file:

HTML
public SortDirection GridViewSortDirection
       {
           get
           {
               if (ViewState["sortDirection"] == null)
                   ViewState["sortDirection"] = SortDirection.Ascending;
               return (SortDirection)ViewState["sortDirection"];
           }
           set
           {
               ViewState["sortDirection"] = value;
           }
       }

       protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
       {
           string sortExpression = e.SortExpression;
           if (GridViewSortDirection == SortDirection.Ascending)
           {
               GridViewSortDirection = SortDirection.Descending;
               SortGridView(sortExpression, DESCENDING);
           }
           else
           {
               GridViewSortDirection = SortDirection.Ascending;
               SortGridView(sortExpression, ASCENDING);
           }
       }

       private void SortGridView(string sortExpression, string direction)
       {
           DataTable dt = data.DisplayData();
           DataView dv = new DataView(dt);
           dv.Sort = sortExpression + direction;
           GridView1.DataSource = dv;
           GridView1.DataBind();
       }


datalayer code:

C#
public DataTable DisplayData()
        {
            SqlConnection sqlCon = new SqlConnection(Con);
            SqlCommand sqlcmd = new SqlCommand(SELECT,sqlCon);
            sqlcmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter ada = new SqlDataAdapter(sqlcmd);
            DataTable dt = new DataTable();
            ada.Fill(dt);
            return dt;
        }



stored procedure:

SQL
ALTER PROCEDURE [dbo].[DisplayData]

AS
BEGIN

select
        A.Emp_ID,
        EmpName=(B.First_Name+' '+ isnull(B.Middle_Name,'') +' ' +B.Last_Name),
        D.designation as Designation,
        A.Website_Access,
        A.Gift_Selection,
        A.Shipping_Address,
        A.Gift_Tracking,
A.Submit_date

    from OnlineGiftingSurvey A, mtw02ipdb03.PFSItedb.dbo.employees B,
            mtw02ipdb03.PFSItedb.dbo.emp_designation C,
            mtw02ipdb03.PFSItedb.dbo.designation_master D
    where
    A.Emp_ID=B.Domain_user_name
    And C.emp_id=B.Emp_id
    And D.desig_id=C.desig_id
END
Posted
Updated 27-Feb-12 19:13pm
v2
Comments
Sergey Alexandrovich Kryukov 28-Feb-12 0:12am    
Did you look at you post by yourself? I mean formatting? Do you think it's readable?
(I unchecked the check box "Treat my content as plain text, not as HTML", please format C# code the same way as the ASP.)
--SA

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