GridView绑定数据源List列排序

GridView,GirdList绑定数据源是泛型集合List<T>,对GridView进行列排序,使用List<T>自带的委托排序,实现方法如下(适用于C#2.0及以上语法):

 

ASPX页面的HTML源码:

 

<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvList" runat="server" AutoGenerateColumns="False" CellPadding="4"
            ForeColor="#333333" GridLines="None" AllowSorting="true" OnSorting="gvList_Sorting">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="序号" SortExpression="ID" />
                <asp:BoundField DataField="Name" HeaderText="姓名" SortExpression="Name" />
                <asp:BoundField DataField="Height" HeaderText="身高" SortExpression="Height" />
                <asp:BoundField DataField="Weight" HeaderText="体重" SortExpression="Weight" />
                <asp:BoundField DataField="Salary" HeaderText="薪水" SortExpression="Salary" />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
    </div>
    </form>
</body>

 

 

对应的C#代码:

 

 
namespace ZhangZhaoLocalDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        /// <summary>
        /// 排序条件:0升序,1降序
        /// </summary>
        protected int OrderType
        {
            get
            {
                return ViewState["OrderType"] == null ? 0 : Convert.ToInt32(ViewState["OrderType"]);
            }
            set
            {
                ViewState["OrderType"] = value;
            }
        }

        /// <summary>
        /// 排序字段
        /// </summary>
        public string OrderField
        {
            get
            {
                return ViewState["OrderField"] == null ? "ID" : ViewState["OrderField"].ToString();
            }
            set
            {
                ViewState["OrderField"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridViewBind();
            }
        }

        protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (this.OrderField == e.SortExpression)
            {
                if (OrderType == 0)
                    OrderType = 1;
                else
                    OrderType = 0;
            }

            this.OrderField = e.SortExpression;

            GridViewBind();
        }

        private void GridViewBind()
        {
            List<TeamMemberEntity> list = this.CreateDataSource();

            list.Sort(delegate(TeamMemberEntity entity1, TeamMemberEntity entity2)
            {
                switch (this.OrderField)
                {
                    case "Name":
                        {
                            return this.OrderType == 0 ?
                                Comparer<string>.Default.Compare(entity1.Name, entity2.Name) :
                                Comparer<string>.Default.Compare(entity2.Name, entity1.Name);
                        }
                    case "Height":
                        {
                            return this.OrderType == 0 ?
                                Comparer<int>.Default.Compare(entity1.Height, entity2.Height) :
                                Comparer<int>.Default.Compare(entity2.Height, entity1.Height);
                        }
                    case "Weight":
                        {
                            return this.OrderType == 0 ?
                                Comparer<double>.Default.Compare(entity1.Weight, entity2.Weight) :
                                Comparer<double>.Default.Compare(entity2.Weight, entity1.Weight);
                        }
                    case "Salary":
                        {
                            return this.OrderType == 0 ?
                                Comparer<decimal>.Default.Compare(entity1.Salary, entity2.Salary) :
                                Comparer<decimal>.Default.Compare(entity2.Salary, entity1.Salary);
                        }

                    default:
                        {
                            return this.OrderType == 0 ?
                                Comparer<int>.Default.Compare(entity1.ID, entity2.ID) :
                                Comparer<int>.Default.Compare(entity2.ID, entity1.ID);
                        }
                }
            });

            this.gvList.DataSource = list;
            this.gvList.DataBind();
        }

        private List<TeamMemberEntity> CreateDataSource()
        {
            List<TeamMemberEntity> list = new List<TeamMemberEntity>();

            Random randome = new Random();

            for (int i = 0; i < 10; i++)
            {
                list.Add(new TeamMemberEntity()
                {
                    ID = i + 1,
                    Name = "MemberName" + i.ToString(),
                    Height = 150 + randome.Next(0, 20) - i,
                    Weight = 50 + randome.Next(0, 50) + i,
                    Salary = randome.Next(10000, 30000)
                });
            }

            return list;
        }
    }

    public class TeamMemberEntity
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Height { get; set; }
        public double Weight { get; set; }
        public decimal Salary { get; set; }

    }
}

 

你可能感兴趣的:(GridView绑定数据源List列排序)