Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 31-Jan-13 3:36am
Edited 31-Jan-13 3:43am
v4
Comments
Abhishek Pant at 31-Jan-13 9:43am
   
Marcus Kramer at 31-Jan-13 9:56am
   
+5. You should post the link as the answer here.
Abhishek Pant at 31-Jan-13 10:08am
   
thanks. also thanks for your suggestion.Posted it(below) with more links.
Sandeep Mewara at 31-Jan-13 9:51am
   
And the issue when you tried to do it...?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
Sergey Alexandrovich Kryukov at 31-Jan-13 10:34am
   
Right, a 5.
—SA
Abhishek Pant at 31-Jan-13 10:40am
   
Thankyou, Sergey!
Marcus Kramer at 31-Jan-13 10:47am
   
My 5 as well.
Abhishek Pant at 31-Jan-13 10:59am
   
thanks!
Member 8090436 at 1-Feb-13 7:02am
   
Hi,
This link not fulfil my requirement.
Can you suggest any other approaced!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 31-Jan-13 10:33am
   
Sure, a 5.
—SA
Espen Harlinn at 2-Feb-13 9:21am
   
Thank you, Sergey :-D
Marcus Kramer at 31-Jan-13 10:47am
   
+5.
Espen Harlinn at 2-Feb-13 9:22am
   
Thank you, Marcus :-D
Member 8090436 at 1-Feb-13 7:01am
   
Hi,
This will not fruitful for my requirement.
Can you suggest any other approached!
Espen Harlinn at 1-Feb-13 7:09am
   
Based on your btnCollection_Click method, it seems like a 100% match. If you're saying that it's not, you need to provide some more information.

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



Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100