如何将SORTING应用于C#.net中的LIST COLLECTION? [英] How to apply SORTING to LIST COLLECTION in C#.net ?

查看:93
本文介绍了如何将SORTING应用于C#.net中的LIST COLLECTION?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我突出显示的代码,我想在任何列上应用排序 - >

Below are my Highlighted code there i want to apply sorting on any column-->

{
    public class SalesPlanProperty
    {
        private int _ID;
        private string _fieldName;
        private string _valueType;
        private DateTime _JoiningDate;
        public int ID
        {
            get { return _ID; }
            set{_ID=value;}
        }
        public string fieldName
        {
            get{ return _fieldName;}
            set{_fieldName = value;}
        }
        public string ValueType
        {
            get { return _valueType; }
            set { _valueType = value; }
        }
        public DateTime JoiningDate
        {
            get { return _JoiningDate; }
            set { _JoiningDate = value; }
        }
    }
}
List<SalesPlanProperty> propertyList = new List<SalesPlanProperty>();
        protected void Page_Load(object sender, EventArgs e)
        {  }
        public void SortdListByParam(object strFiled)
        {
            propertyList.OrderBy(x => strFiled).ToList(); // HERE I WANT TO SORT MY COLLECTION ON ANY PROPERTY VAILABLE IN MY CLASS.</big>
        }

        public List<SalesPlanProperty> CreateCollection()
        {
            for (int i = 1; i <= 500; i++)
            {
                var data = new SalesPlanProperty { ID = i, fieldName = "Field" + i.ToString(), ValueType = "Value" + i.ToString(), JoiningDate = DateTime.Now.AddSeconds(i)  };
                propertyList.Add(data);
            }
            return propertyList;
        }

        public void getdata(int pageNo, object myDictionary,object strSortFieldName)
        {
            List<SalesPlanProperty> KeyValuePair=((List<SalesPlanProperty>) myDictionary);
            GridView1.DataSource = KeyValuePair;
            GridView1.DataBind();
            int low = (pageNo - 1) * 100;
            int high = low + 100;
            this.SortdListByParam(strSortFieldName);
            foreach(SalesPlanProperty spp in KeyValuePair)
            {
                int Idvalue = spp.ID;
                if (Idvalue > low && Idvalue <= high)
                {
                    Response.Write("ID " + Idvalue + "          ");
                    Response.Write("FieldName  " + spp.fieldName + "     ");
                    Response.Write("Value   " +spp.ValueType + "       ");
                }
            }
        }

        protected void btnCollection_Click(object sender, EventArgs e)
        {
            object strSortFieldNameValue = "JoiningDate";//Pass column name on which it is sort.
            this.getdata(2, this.CreateCollection(), strSortFieldNameValue);//Pass Page number index,Create collection and column name on which it is sort.
        }

       protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            List<SalesPlanProperty> KeyValuePair = this.CreateCollection();
            GridView1.DataSource = KeyValuePair;
            GridView1.DataBind();
        }
    }

推荐答案

使用C#列表排序 [ ^ ]

快速列表< string>用C#排序 [ ^ ]

自定义按任意顺序排列对象列表 [ ^ ]
Sorting using C# Lists[^]
Fast List<string> Sort in C#[^]
Custom Sort a List of Objects by Any Order[^]


如果我正确理解您的问题,您希望允许用户选择条件用于分类。 .Net通过一组接口提供通用机制:

If I understand your question correctly, you want to allow the user to choose the criteria for sorting. .Net provides a generic mechanism through a set of interfaces:

  • IBindingList[^]
  • IBindingListView[^]


这篇关于如何将SORTING应用于C#.net中的LIST COLLECTION?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆