Gridview sorting

Santosh0683
Posted by Santosh0683 under ASP.NET category on | Views : 2697
public partial class _Default : System.Web.UI.Page
{
private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
protected void Page_Load(object sender, EventArgs e)
{

lbl.ForeColor = System.Drawing.Color.Green;
if (!IsPostBack)
{

SortGridView(sortExpression, "");

}
}


public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;

return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}

protected void gvCust_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);
}

}

static string sortExpression;
private void SortGridView(string sortExpression, string direction)
{
SqlConnection con;
con = new SqlConnection(ConfigurationManager.ConnectionStrings["StrConnect"].ToString());
con.Open();
SqlDataAdapter oAdapter = new SqlDataAdapter("usp_GetCustDetail", con);
oAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet oDataSet = new DataSet();
oAdapter.Fill(oDataSet);

DataTable dt = oDataSet.Tables[0];

DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;

gvCust.DataSource = dv;
gvCust.DataBind();
}
}

Comments or Responses

Login to post response