Click here to Skip to main content
15,886,810 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();
        }
    }
Posted
Updated 31-Jan-13 3:43am
v4
Comments
Abhishek Pant 31-Jan-13 9:43am    
fjdiewornncalwe 31-Jan-13 9:56am    
+5. You should post the link as the answer here.
Abhishek Pant 31-Jan-13 10:08am    
thanks. also thanks for your suggestion.Posted it(below) with more links.
Sandeep Mewara 31-Jan-13 9:51am    
And the issue when you tried to do it...?

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jan-13 10:34am    
Right, a 5.
—SA
Abhishek Pant 31-Jan-13 10:40am    
Thankyou, Sergey!
fjdiewornncalwe 31-Jan-13 10:47am    
My 5 as well.
Abhishek Pant 31-Jan-13 10:59am    
thanks!
Member 8090436 1-Feb-13 7:02am    
Hi,
This link not fulfil my requirement.
Can you suggest any other approaced!
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:

that allows you to implement this kind of functionality in a manner that works well with the rest of the framework.

These two articles shows how to implement sorting and filtering:
Improvements to Windows Forms Data Binding in the .NET Framework 2.0, Part 2[^]
Implementing multi-column filtering on the IBindingListView[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jan-13 10:33am    
Sure, a 5.
—SA
Espen Harlinn 2-Feb-13 9:21am    
Thank you, Sergey :-D
fjdiewornncalwe 31-Jan-13 10:47am    
+5.
Espen Harlinn 2-Feb-13 9:22am    
Thank you, Marcus :-D
Member 8090436 1-Feb-13 7:01am    
Hi,
This will not fruitful for my requirement.
Can you suggest any other approached!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900