I have my Gridview
developed manually, the problem is that when I click to make update
my gridview
stops being sorted and returns to normal. I intended this not to happen.
Here's how it's set:
protected void CarregaGV(DateTime data,int tipo,string tipoSande)
{
DataTable dt = null;
dt = TrataDados.GetDetailsProductLayout(data, tipo,tipoSande);
Session["TaskTable"] = dt;
GridViewLayoutProduto.DataSource = dt;
GridViewLayoutProduto.DataBind();
}
private string GetSortDirection(string column)
{
// By default, set the sort direction to ascending.
string sortDirection = "ASC";
// Retrieve the last column that was sorted.
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
// Check if the same column is being sorted.
// Otherwise, the default value can be returned.
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
// Save new values in ViewState.
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
protected void GridViewLayoutProduto_Sorting(object sender, GridViewSortEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = Session["TaskTable"] as DataTable;
if (dt != null)
{
//Sort the data.
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
//Session["TaskTable"] = ((DataView)GridViewLayoutProduto.DataSource).ToTable();
GridViewLayoutProduto.DataSource = Session["TaskTable"];
GridViewLayoutProduto.DataBind();
}
}
protected void GridViewLayoutProduto_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewLayoutProduto.EditIndex = e.NewEditIndex;
//CheckBox alt = GridViewLayoutProduto.Rows[e.NewEditIndex].FindControl("CheckBox1") as CheckBox;
//alt.Enabled = false;
BindData();
}
protected void GridViewLayoutProduto_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewLayoutProduto.PageIndex = e.NewPageIndex;
GridViewLayoutProduto.DataBind();
}
protected void GridViewLayoutProduto_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridViewLayoutProduto.EditIndex = -1;
BindData();
}
private void BindData()
{
GridViewLayoutProduto.DataSource = Session["TaskTable"];
GridViewLayoutProduto.DataBind();
}
I think the problem is in Session
["TaskTable"] because I always keep the Grid state in the first place, I think.