Sorting is changed when i go to next page !

Posted by Sahoo.satya1984 under ASP.NET on 2/18/2008 | Views : 2178 | Status : [Member] | Replies : 3
I can sort a column on the first page,but when i go to second page through paging sorting changed it's order.
Another problem is When i click Edit button,sorting is changed as a result data are also changed for which i can't edit the actual row rather i see a different data on the same row to be updated.

Here is my code...

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
OnPageIndexChanging="pagemethod" OnSorting="sortmethod" OnRowEditing="editmethod"
AutoGenerateColumns="False" PageSize="4" >

<Columns>
<asp:CommandField HeaderText="Edit" ShowEditButton="True" ButtonType="Button" />

<asp:TemplateField HeaderText="Name" SortExpression="Name">

<ItemTemplate><%#Eval("Name")%></ItemTemplate>

<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

protected void Page_Load(object sender, EventArgs e)

{
if (!IsPostBack)
{
BindGridView();
}

}

public void BindGridView()

{
DataSet ds = new DataSet();
ds=binddata();
GridView1.DataSource = ds;
GridView1.DataBind();
}

public DataSet binddata()

{
SqlConnection con = new SqlConnection("Server=.;database=emp;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select * from job", con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "emp");
return ds;
}



public void sortmethod(object sender, GridViewSortEventArgs e)

{
ViewState["sortcolumn"] = e.SortExpression;

if (ViewState["sortorder"] == null)
{
ViewState["sortorder"] = "asc";
}
ViewState["sortorder"]=ViewState["sortorder"].ToString() == "asc"?"desc":"asc";

DataView dv = new DataView(binddata().Tables[0]);
dv.Sort = ViewState["sortcolumn"] + " " + ViewState["sortorder"];
GridView1.DataSource = dv;
GridView1.DataBind();
}


public void pagemethod(object sender, GridViewPageEventArgs e)

{
GridView1.PageIndex = e.NewPageIndex;
GridView1.EditIndex = -1;
BindGridView();

}

public void editmethod(object sender, GridViewEditEventArgs e)

{
GridView1.EditIndex = e.NewEditIndex;
BindGridView();


}




Responses

Posted by: Jayakumars on: 9/30/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
yes fetch to assign session for asc or desc value then goto other page thats good way.

Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com

Sahoo.satya1984, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Deysomnath on: 2/20/2008 [Member] Starter

Up
0
Down
Change the code of "binddata()" method as mention below

public DataView Binddata()
{
SqlConnection con = new SqlConnection("Server=.;database=emp;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select * from job", con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "emp");

DataView dv = new DataView(ds.Tables[0]);

if (ViewState["sortorder"] == null)
{
ViewState["sortorder"] = "asc";
}

if (ViewState["sortcolumn"] == null)
{
ViewState["sortcolumn"] = "DefaultColumn"; // use a default sort column name
}


dv.Sort = ViewState["sortcolumn"] + " " + ViewState["sortorder"];
return dv;
}


Sahoo.satya1984, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sahoo.satya1984 on: 2/22/2008 [Member] Starter

Up
0
Down
Thank you Deysomnath
Your code helped me,now i can sort without any problem.

Sahoo.satya1984, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response