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:
<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">
<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:
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:
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:
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