Write the following code In .aspx page :
<asp:GridView ID="gvstudents" runat="server" AllowSorting="true" 
    OnSorting="gvstudents_sorting">
    </asp:GridView>
Code in .aspx.cs:
public partial class SortingEx : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            string strcon = "data source=name of sql server ;initial catalog=database name;integrated security=sspi or user id=sqlserver userid ; password=somepassword";
            string sConnection = strcon;
            DataSet ds = new DataSet();
            SqlConnection cn = new SqlConnection(sConnection);
            using (cn)
            {
                SqlCommand cmd = new SqlCommand("select * from employees", cn);
                using (cmd)
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    da.Fill(ds);
                    gvstudents.DataSource = ds;
                    gvstudents.DataBind();
                    Session["ds"] = ds;
                    ViewState["sortcolumn"] = string.Empty;
                    ViewState["sortdirection"] = string.Empty;
                }
            }
        }
    }
    protected void gvstudents_sorting(object sender, GridViewSortEventArgs e)
    {
        if (Session["ds"] != null)
        {
           DataSet ds = (DataSet)Session["ds"];
           DataTable dt = ds.Tables[0];
           DataView dv = dt.DefaultView;//DataView ado.net view in t-sql 
            if (ViewState["sortcolumn"].ToString() == e.SortExpression.ToString())
            {
                if ("asc" == ViewState["sortdirection"].ToString())
                {
                    ViewState["sortdirection"] = "DESC";
                }
                else
                {
                    ViewState["sortdirection"] = "asc";
                }
            }
            else
            {
                ViewState["sortcolumn"] = e.SortExpression.ToString();
                ViewState["sortdirection"] = "asc";
            }
            if (dv != null)
            {
                dv.Sort = e.SortExpression + " " + ViewState["sortdirection"].ToString();
                //dv.Sort= "ename asc";
                gvstudents.DataSource = dv;
                gvstudents.DataBind();
            }
        }
    }
}
Table structure :
| eid | fname | lname | age | salary | dept | doj | 
|---|---|---|---|---|---|---|
| 1 | rajeev | sukla | 23 | 12000 | .net | 23-Oct-11 12:00:00 AM | 
| 2 | sowmya | kumari | 23 | 19000 | db | 13-Nov-10 12:00:00 AM | 
| 3 | kishore | kumar | 27 | 36000 | android | 16-Oct-11 12:00:00 AM | 
| 4 | abimanyu | biswal | 22 | null | android | 20-Feb-10 12:00:00 AM | 
| 5 | soni | kumar | 24 | 21800 | .net | 21-Jun-09 12:00:00 AM | 
| 6 | anu | _singh | 22 | 12000 | db | 23-Oct-10 12:00:00 AM | 
| 7 | _dinesh | moh%anty | 23 | 15000 | .net | 26-Aug-09 12:00:00 AM | 
| 8 | nishala | _kumari | 22 | 18000 | db | 19-Jul-08 12:00:00 AM | 
| 1 | rajeev | sukla | 23 | 12000 | .net | 23-Oct-11 12:00:00 AM | 
 
 
No comments:
Post a Comment