Missing sorting when changed page in C# - c#

I need to sort a column in a GridView and this worked but I've problem when I change pages in GridView because missing the sorting, can you help me?
Below is my code C# for sorting column in GridView, thanks in advance.
public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
sortingDirection = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
sortingDirection = "Desc";
}
else
{
dir = SortDirection.Ascending;
sortingDirection = "Asc";
}
DataView sortedView = new DataView(GridViewBind());
sortedView.Sort = e.SortExpression + " " + sortingDirection;
PleaseWait();
GridView1.DataSource = sortedView;
GridView1.DataBind();
}
public DataTable GridViewBind()
{
sql1 = " SELECT * FROM `tbl` ORDER BY empid DESC; ";
dadapter = new OdbcDataAdapter(sql1, myConnectionString);
dset = new DataSet();
dset.Clear();
dadapter.Fill(dset);
DataTable dt=dset.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
dadapter.Dispose();
dadapter = null;
myConnectionString.Close();
return dt;
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewBind();
GridView1.DataSource = dset.Tables[0];
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void ddlPages_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow gvrPager = GridView1.BottomPagerRow;
DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
GridViewBind();
GridView1.DataSource = dset.Tables[0];
GridView1.PageIndex = ddlPages.SelectedIndex;
GridView1.DataBind();
}
protected void Paginate(object sender, CommandEventArgs e)
{
int intCurIndex = GridView1.PageIndex;
switch (e.CommandArgument.ToString().ToLower())
{
case "First":
GridView1.PageIndex = 0;
break;
case "Prev":
GridView1.PageIndex = intCurIndex - 1;
break;
case "Next":
GridView1.PageIndex = intCurIndex + 1;
break;
case "Last":
GridView1.PageIndex = GridView1.PageCount - 1;
break;
}
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");
if (lblPageCount != null)
lblPageCount.Text = GridView1.PageCount.ToString();
for (int i = 1; i <= GridView1.PageCount; i++)
{
ddl.Items.Add(i.ToString());
}
ddl.SelectedIndex = GridView1.PageIndex;
if (GridView1.PageIndex == 0)
{
((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
}
if (GridView1.PageIndex + 1 == GridView1.PageCount)
{
((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
}
}
}
EDIT 1
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
sortingDirection = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
sortingDirection = "Desc";
}
else
{
dir = SortDirection.Ascending;
sortingDirection = "Asc";
}
DataView sortedView = new DataView(GridViewBind());
sortedView.Sort = e.SortExpression + " " + sortingDirection;
PleaseWait();
GridViewBind();
GridView1.DataSource = dset.Tables[0];
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
EDIT 2
public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
public string SortField
{
get
{
return (string)ViewState["SortField"] ?? "empid"; // default sort
}
set
{
ViewState["SortField"] = value;
}
}
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
sortingDirection = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
sortingDirection = "Desc";
}
else
{
dir = SortDirection.Ascending;
sortingDirection = "Asc";
}
DataView sortedView = new DataView(GridViewBind());
sortedView.Sort = e.SortExpression + " " + sortingDirection;
SortField = e.SortExpression;
PleaseWait();
GridView1.DataSource = sortedView;
GridView1.DataBind();
}
public DataTable GridViewBind()
{
sql1 = " SELECT * FROM `tbl` ORDER BY empid DESC; ";
dadapter = new OdbcDataAdapter(sql1, myConnectionString);
dset = new DataSet();
dset.Clear();
dadapter.Fill(dset);
DataTable dt=dset.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
dadapter.Dispose();
dadapter = null;
myConnectionString.Close();
return dt;
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
sortingDirection = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
sortingDirection = "Desc";
}
else
{
dir = SortDirection.Ascending;
sortingDirection = "Asc";
}
DataView sortedView = new DataView(GridViewBind());
sortedView.Sort = SortField + " " + sortingDirection;
PleaseWait();
GridViewBind();
GridView1.DataSource = dset.Tables[0];
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void ddlPages_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow gvrPager = GridView1.BottomPagerRow;
DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
GridViewBind();
GridView1.DataSource = dset.Tables[0];
GridView1.PageIndex = ddlPages.SelectedIndex;
GridView1.DataBind();
}
protected void Paginate(object sender, CommandEventArgs e)
{
int intCurIndex = GridView1.PageIndex;
switch (e.CommandArgument.ToString().ToLower())
{
case "First":
GridView1.PageIndex = 0;
break;
case "Prev":
GridView1.PageIndex = intCurIndex - 1;
break;
case "Next":
GridView1.PageIndex = intCurIndex + 1;
break;
case "Last":
GridView1.PageIndex = GridView1.PageCount - 1;
break;
}
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");
if (lblPageCount != null)
lblPageCount.Text = GridView1.PageCount.ToString();
for (int i = 1; i <= GridView1.PageCount; i++)
{
ddl.Items.Add(i.ToString());
}
ddl.SelectedIndex = GridView1.PageIndex;
if (GridView1.PageIndex == 0)
{
((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
}
if (GridView1.PageIndex + 1 == GridView1.PageCount)
{
((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
}
}
}
EDIT 3
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
sortingDirection = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
sortingDirection = "Desc";
}
else
{
dir = SortDirection.Ascending;
sortingDirection = "Asc";
}
DataView sortedView = new DataView(GridViewBind());
sortedView.Sort = SortField + " " + sortingDirection;
PleaseWait();
GridViewBind();
GridView1.DataSource = sortedView;
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}

When GridView1_PageIndexChanging() is executing you do not sort the data. To fix it, you need to add to that method same code as in gridView_Sorting() but let GridView1.PageIndex
Update:
To remember the sort field add a new property
public string SortField
{
get {
return (string)ViewState["SortField"] ?? "empid"; // default sort
}
set {
ViewState["SortField"]=value;
}
}
Set it in the sorting event gridView_Sorting() as
SortField = e.SortExpression;
Now, use it everywhere where you need to get the sort column, e.g. in GridView1_PageIndexChanging()
//sortedView.Sort = e.SortExpression + " " + sortingDirection;
sortedView.Sort = SortField + " " + sortingDirection;
Update 2:
There are 2 more problems in EDIT3 in the GridView1_PageIndexChanging Method
you bind to a wrong data source
you change sorting order to an opposite direction
Must be
//sortingDirection = string.Empty;
if (dir == SortDirection.Ascending)
{
//dir = SortDirection.Descending;
sortingDirection = "Asc"; // "Desc";
}
else
{
//dir = SortDirection.Ascending;
sortingDirection = "Desc"; // "Asc";
}
...
GridView1.DataSource = sortedView; //dset.Tables[0];
That's should help but in general you code is very insufficient and you might need to look for best practice examples in the internet.

Related

GridView Sorting in ASP.NET 2 using C#

I need GridView Sorting in ASP.NET 2 using C# and tried this tutorial:
http://www.dotnetfox.com/articles/gridview-sorting-example-in-Asp-Net-using-C-Sharp-1082.aspx
Below is my code C# net 2 for sorting column in GridView but I've this error:
Exception Details: System.IndexOutOfRangeException: Cannot find table 0.
in this line why?
Line 39: GridViewBind();
Line 40: DataTable dt = new DataTable();
Line 41: dt = ds.Tables[0];
Line 42: {
Line 43: string SortDir = string.Empty;
DataSet ds = new DataSet();
public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
protected void gvEmployee_Sorting(object sender, GridViewSortEventArgs e)
{
GridViewBind();
DataTable dt = new DataTable();
dt = ds.Tables[0];
{
string SortDir = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
SortDir = "Desc";
}
else
{
dir = SortDirection.Ascending;
SortDir = "Asc";
}
DataView sortedView = new DataView(dt);
sortedView.Sort = e.SortExpression + " " + SortDir;
GridView1.DataSource = sortedView;
GridView1.DataBind();
}
}
public void GridViewBind()
{
SQL = " SELECT * FROM doTable; ";
try
{
conn.Open();
dadapter = new OdbcDataAdapter(SQL, conn);
dset = new DataSet();
dset.Clear();
dadapter.Fill(dset);
GridView1.DataSource = dset.Tables[0];
GridView1.DataBind();
}
catch (Exception ee)
{
throw ee;
}
finally
{
conn.Close();
conn.Dispose();
}
}
EDIT 1
using System;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Av_Default : System.Web.UI.Page
{
OdbcConnection conn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cs"].ConnectionString);
string SQL;
OdbcDataAdapter dadapter;
DataSet dset;
DataTable dt = new DataTable();
DataSet ds = new DataSet();
public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
protected void gvEmployee_Sorting(object sender, GridViewSortEventArgs e)
{
GridViewBind();
DataTable dt = new DataTable();
dt = ds.Tables[0];
{
string SortDir = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
SortDir = "Desc";
}
else
{
dir = SortDirection.Ascending;
SortDir = "Asc";
}
DataView sortedView = new DataView(dt);
sortedView.Sort = e.SortExpression + " " + SortDir;
GridView1.DataSource = sortedView;
GridView1.DataBind();
}
}
public void GridViewBind()
{
SQL = " SELECT * FROM doTAble; ";
try
{
conn.Open();
dadapter = new OdbcDataAdapter(SQL, conn);
dset = new DataSet();
dset.Clear();
dadapter.Fill(dset);
GridView1.DataSource = dset.Tables[0];
GridView1.DataBind();
}
catch (Exception ee)
{
throw ee;
}
finally
{
conn.Close();
conn.Dispose();
}
}
protected void gvEmployee_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = GetRows();
{
string SortDir = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
SortDir = "Desc";
}
else
{
dir = SortDirection.Ascending;
SortDir = "Asc";
}
DataView sortedView = new DataView(dt);
sortedView.Sort = e.SortExpression + " " + SortDir;
GridView1.DataSource = sortedView;
GridView1.DataBind();
}
}
public void GridViewBind(DataTable dt){
GridView1.DataSource=dt;
GridView1.DataBind();
}
public DataTable GetRows()
{
var SQL = " SELECT * FROM doTAble; ";
//You should use the "using" resource acquisition statement
// http://www.dotnetperls.com/sqlconnection
using(var conn=new OdbcConnection(ConfigurationManager.ConnectionStrings["cs"].ConnectionString))
{
conn.Open();
var dadapter = new OdbcDataAdapter(SQL, conn);
var dset = new DataSet();
dadapter.Fill(dset);
return dset.Tables[0];
}
}

GridView sorting ascending is not working

I have a GridView with sorting enabled. For a single page result both the ascending and descending works fine. But when there are multiple pages, descending alone works well. the asceding also works but when i click on the next pages, it becomes unsorted again.
I don't know whether the problem is because of the sort direction or paging. Kindly Help.
Below are the codes:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
GridView grid = sender as GridView;
//Retrieve the table from the session object.
DataTable dt = Session["List"] as DataTable;
if (dt != null)
{
//Sort the data.
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
grid.DataSource = Session["List"];
grid.DataBind();
}
}
private string GetSortDirection(string column)
{
string sortDirection = "ASC";
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
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 GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = bindGridView();
GridView1.DataBind();
}
You can use a session variable to store the latest Sort Expression and when you sort the grid next time compare the sort expression of the grid with the Session variable which stores last sort expression. If the columns are equal then check the direction of the previous sort and sort in the opposite direction.
DataTable sourceTable = GridAttendence.DataSource as DataTable;
DataView view = new DataView(sourceTable);
string[] sortData = Session["sortExpression"].ToString().Trim().Split(' ');
if (e.SortExpression == sortData[0])
{
if (sortData[1] == "ASC")
{
view.Sort = e.SortExpression + " " + "DESC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "DESC";
}
else
{
view.Sort = e.SortExpression + " " + "ASC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";
}
}
else
{
view.Sort = e.SortExpression + " " + "ASC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";
}
After so many research, I found the correct answer for my own question. This would probably help someone in future. Add the following code to the bindGridView()
if (ViewState["sortExpr"] != null)
{
dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortExpr"];
}
else
dv = ds.Tables[0].DefaultView;
and
#region Sorting
protected void Gridview1_Sort(object sender, GridViewSortEventArgs e)
{
ViewState["sortExpr"] = e.SortExpression + " " + GetSortDirection(e.SortExpression);
Gridview1.DataSource = bindGridView();
Gridview1.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;
}
#endregion

How to sorting the gridview data

See the below code, I am try to sorting Grid view data by calling function provided by the Microsoft vs 2008, but the paging is done well but the sorting process is not work, tell me where should i change the following code and yes i put grid view in updated penal,is there problem regarding to update penal?
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
showData();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
SqlCommand cmd = new SqlCommand("showData", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0] != null)
{
ds.Tables[0].DefaultView.Sort = e.SortExpression + " " + sortType(e.SortDirection);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
}
}
private string sortType(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "DESC";
break;
case SortDirection.Descending:
newSortDirection = "ASC";
break;
}
return newSortDirection;
}
Can you ensure AllowSorting="true" and OnSorting="GridView1_Sorting"
Also refer
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx
http://www.codeproject.com/Articles/26882/Gridview-Sorting-with-Up-and-Down-Icons-Paging
sorting and paging with gridview asp.net
http://www.c-sharpcorner.com/uploadfile/afenster/gridview-sorting/
Hope this helps
i think you have to assign dataview to the DataSource property of gridview, not the dataset object. like this
DataTable dt = ds.Tables[0];
DataView dv = new DataView(dt);
dv.Sort = e.SortExpression + " " + sortType(e.SortDirection);
GridView1.DataSource = dv;
GridView1.DataBind();
you are missing a default value for newSortDirection.
private string sortType(SortDirection sortDirection)
{
string newSortDirection = "ASC";
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "DESC";
break;
case SortDirection.Descending:
newSortDirection = "ASC";
break;
}
return newSortDirection;
}

How to sorting the gridview when i click the header text on gridview in asp.net

I have developed an asp.net application that has a Gridview control. I want it to sort when I click on the Gridview header text in asp.net. Entity framework is used to bind the gridview.
protected void grdmortgagesaver_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)
{
// You can cache the DataTable for improving performance
var databind = from i in mortgageentites.Clients orderby i.LastName select i;
if (databind.Count() > 0)
{
grdmortgagesaver.DataSource = databind.ToList();
grdmortgagesaver.DataBind();
DataTable dt = clients;
if (dt != null)
{
// DataTable dt = ds.Tables[0];
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
grdmortgagesaver.DataSource = dv;
grdmortgagesaver.DataBind();
}
}
}
If you are using a standard ASP.NET System.Web.UI.WebControls.GridView, you can simply "Enable Sorting", in the properties pane by setting "AllowSorting" to true or via the "pop-out" on the top right of the control by checking "Enable Sorting"
add AllowSorting="true" in your gridview control and
protected void grd_Sorting(object sender, GridViewSortEventArgs e)
{
if (e.SortExpression == hdfSortExp.Value)
{
if (hdfUpDown.Value == "1")
hdfUpDown.Value = "0";
else
hdfUpDown.Value = "1";
}
else //New Column clicked so the default sort direction will be incorporated
hdfUpDown.Value = "0";
hdfSortExp.Value = e.SortExpression; //Update the sort column
BindGrid(hdfSortExp.Value, this.CBool(hdfUpDown.Value));
}
in your c# code
public void BindGrid(string sortBy, bool inAsc)
{
grd.DataSource = WManager.GetAdminTags(txtSearch.Text.Trim(), sortBy, inAsc);
grd.DataBind();
}
First in your Page in the GridView Declaration Tag add the property AllowSorting="true" and in the Gridview_SortCommand try the Following code as guidence
try {
if (SortType == false) {
SortView.Sort = e.SortExpression + " ASC";
//dviewGeneral.Sort = e.SortExpression & " ASC"
Session.Item("SortOrder") = SortView.Sort.ToString;
SortType = true;
} else {
SortView.Sort = e.SortExpression + " DESC";
Session.Item("SortOrder") = SortView.Sort.ToString;
//dviewGeneral.Sort = e.SortExpression & " DESC"
SortType = false;
}
CMSgrid.SelectedIndex = -1;
CMSgrid.CurrentPageIndex = 0;
SortBind();
} catch (Exception ex) {
UserMsgBox(ex.ToString());
}

Sorting gridview

I have a gridview where I bind a datasource, and I had to add sorting for this gridview; I added the code below to that, but it didn't work well.
private string ConvertSortDirectionToSql(SortDirection sortDireciton)
{
string m_SortDirection = String.Empty;
switch (sortDireciton)
{
case SortDirection.Ascending:
m_SortDirection = "ASC";
break;
case SortDirection.Descending:
m_SortDirection = "DESC";
break;
}
return m_SortDirection;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable m_DataTable = GridView1.DataSource as DataTable;
if (m_DataTable != null)
{
DataView m_DataView = new DataView(m_DataTable);
m_DataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
GridView1.DataSource = m_DataView;
GridView1.DataBind();
}
}
You can use this, as I had the same problem, and I solved it like this.
public string SortingExpression
{
get
{
if (this.ViewState["SortExpression"] == null)
return "";
else
return (string)this.ViewState["SortExpression"];
}
set
{
this.ViewState["SortExpression"] = value;
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable m_DataTable = GridView1.DataSource as DataTable;
if (m_DataTable != null)
{
DataView m_DataView = new DataView(m_DataTable);
SortingExpression = e.SortExpression + " " + (SortingExpression.Contains("ASC") ? "DESC" : "ASC");
m_DataView.Sort =SortingExpression;
GridView1.DataSource = m_DataView;
GridView1.DataBind();
}
}
Maybe this could help if your sortdirection is always ascending.
Try this out. This method worked for me.
dt is the datatable containing the values.
protected void onSorting_Gridview1(object sender, GridViewSortEventArgs e)
{
string _sortDirection = dir.ToString();
if(_sortDirection.Equals("Ascending"))
{
_sortDirection = "ASC";
dir = SortDirection.Descending;
}
else
{
_sortDirection="DESC";
dir = SortDirection.Ascending;
}
if (dt != null)
{
//Sort the data.
dt.DefaultView.Sort = e.SortExpression + " " + _sortDirection;
gridView1.DataSource = dt;
gridView1.DataBind();
}
}
public SortDirection dir
{
get
{
if (ViewState["DIR"] == null)
{
ViewState["DIR"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["DIR"];
}
set
{
ViewState["DIR"] = value;
}
}

Categories

Resources