[转]GridView排序——微软提供Sort

本文转自:http://www.cnblogs.com/eva_2010/articles/1995646.html

在GridView中,根据其中的某列进行排序。

1. 页面:AllowSorting=“True” onsorting=“ ”,SortExpression="列名"

 

<asp:GridView ID="grdResult" runat="server" CssClass="gridviewstyle" AutoGenerateColumns="False" 

AllowSorting="True" onsorting="grdResult_Sorting">

         <Columns>
                <asp:CommandField HeaderText="Edit" ShowEditButton="True">
                    <ControlStyle Width="150px" />
                </asp:CommandField>
                <asp:BoundField DataField="Id" HeaderText="ID"  SortExpression="Id">
                    <ControlStyle Width="20px" />
                <HeaderStyle Width="80px" ForeColor="White" />
                </asp:BoundField>
                <asp:BoundField DataField="Name" HeaderText="Name" >
                    <ControlStyle Width="50px" />
                <HeaderStyle Width="120px"  Font-Underline="True"  />
                </asp:BoundField>
            </Columns>

</asp:GridView>

 

2.后台代码:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.grdResult.Attributes.Add("SortExpression", "Id");
            this.grdResult.Attributes.Add("SortDirection", "ASC");
            BindGridInfo();
        }
    }
private List<Test> GetTestData()
    {
        Test test1 = new Test { Id = 1, Name = "Test1" };
        Test test2 = new Test { Id = 2, Name = "Test2" };
        Test test3 = new Test { Id = 3, Name = "Test3" };
        Test test4 = new Test { Id = 4, Name = "Test4" };
        List<Test> lstTest = new List<Test>();
        lstTest.Add(test1);
        lstTest.Add(test2);
        lstTest.Add(test3);
        lstTest.Add(test4);
        return lstTest;
    }
    private DataTable GetData()
    {
        DataTable dtTest = new DataTable();
        dtTest.Columns.Add("Id");
        dtTest.Columns.Add("Name");
        dtTest.Rows.Add(1, "111");
        dtTest.Rows.Add(3, "333");
        dtTest.Rows.Add(2, "222");
        dtTest.Rows.Add(4, "444");

        return dtTest;
    }
    //数据绑定,如果返回数据源是DataTable则可以直接排序,如果不是则要先转换为DataTable格式数据源
    private void BindGridInfo()
    {
        List<Test> lstTest = GetTestData();
        DataTable dt = new DataTable();
        dt.Columns.Add("Id");
        dt.Columns.Add("Name");
        for (int i = 0; i < lstTest.Count; i++)
        {
            DataRow dr = dt.NewRow();
            dr["Id"] = lstTest[i].Id;
            dr["Name"] = lstTest[i].Name;
            dt.Rows.Add(dr);
        }
        string sortExpression = this.grdResult.Attributes["SortExpression"];
        string sortDirection = this.grdResult.Attributes["SortDirection"];

        DataTable dtSource = GetData();
        if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
        {
            dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
        }
        grdResult.DataSource = dt;// dtSource;
        grdResult.DataBind();
    }
Class Test: View Code 

    /// <summary>
    /// define a container class
    /// </summary>
    private class Test
    {
        private int _id;
        /// <summary>
        /// test id
        /// </summary>
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        private string _name;
        /// <summary>
        /// test name
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }
/// <summary>
    /// sorting
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grdResult_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression.ToString();
        string sortDirection = "ASC";
        if (sortExpression == this.grdResult.Attributes["SortExpression"])
        {
            sortDirection = (this.grdResult.Attributes["SortDirection"].ToString() == sortDirection) ? "DESC" : "ASC";
        }
        this.grdResult.Attributes["SortExpression"] = sortExpression;
        this.grdResult.Attributes["SortDirection"] = sortDirection;
        this.BindGridInfo();
    }

 

 参考: http://www.cnblogs.com/heekui/archive/2008/06/02/1212051.html

Be the change you want to see in the world.

 

你可能感兴趣的:([转]GridView排序——微软提供Sort)