开发者

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

开发者 https://www.devze.com 2023-03-15 08:34 出处:网络
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.

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 = SortDire开发者_Go百科ction.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());
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消