Gautam Buddha Temple Varanasi,Varanasi - Buddhist Pilgrimage

Monday, August 2, 2010

Gridview custom sorting using asp.net c#

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)
{
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();
}
}

Thanks & Regards
Santosh

No comments:

Post a Comment