Code for sorting Grid View items on basis of Name or ID or any other column in Visual Studio WebPart( For SharePoint 2010)

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace TimerSharePoint.SortGridView
{

/* Class for get and set value of columns of a list in SharePoint. I have taken these 5 columns */
    class Data
    {
        public string Name { get; set; }
        public string Branch { get; set; }
        public string Roll_No { get; set; }
        public string Address { get; set; }
        public string Mobile_No { get; set; }
    }

    class Datarollasc : IComparer<Data>
    {

/* When your field is of string data type(I have used String above in Data class) */
        public int Compare(Data x, Data y)
        {
            return string.Compare(x.Roll_No, y.Roll_No);
        }
/* When your field is of Int data type(Although I haven't used String above in Data class but if u have int type then use this commented code) */
        //public int Compare(Data x, Data y)
        //{
        //    if (x.Branch > y.Branch) return 1;
        //    else if (x.Branch < y.Branch) return -1;
        //    else return 0;
        //}
    }

    class Datarolldesc : IComparer<Data>
    {
        public int Compare(Data x, Data y)
        {
            return (-1 * string.Compare(x.Roll_No, y.Roll_No));
        }

        //public int Compare(Data x, Data y)
        //{
        //    if (x.Branch < y.Branch) return 1;
        //    else if (x.Branch > y.Branch) return -1;
        //    else return 0;
        //}
    }

    class Datanameasc : IComparer<Data>
    {
        public int Compare(Data x, Data y)
        {
            return string.Compare(x.Name, y.Name);
        }
    }
    class Datanamedesc : IComparer<Data>
    {
        public int Compare(Data x, Data y)
        {
            return (-1 * string.Compare(x.Name, y.Name));
        }
    }
    class Dataaddressasc : IComparer<Data>
    {
        public int Compare(Data x, Data y)
        {
            return string.Compare(x.Address, y.Address);
        }
    }
    class Dataaddressdesc : IComparer<Data>
    {
        public int Compare(Data x, Data y)
        {
            return (-1 * string.Compare(x.Address, y.Address));
        }
    }
    class vary
    {
        static public int count1 = 1;
        static public int count2 = 1;
        static public int count3 = 1;
    }
    public partial class SortGridViewUserControl : UserControl
    {
        SPList info = SPContext.Current.Web.Lists["cust"];
        List<Data> data = new List<Data>();
        protected void Page_Load(object sender, EventArgs e)
        {
                try
                {

                    foreach (SPListItem item in info.Items)
                    {
                        Data d = new Data();
                        d.Roll_No = item["Roll_No."].ToString();
                        d.Name = item["Name"].ToString();
                        d.Mobile_No = item["Mobile_No."].ToString();
                        d.Address = item["Address"].ToString();
                        d.Branch = item["Branch"].ToString();
                        data.Add(d);


                    }
                    GridView1.DataSource = data;
                    GridView1.DataBind();


                }
                catch (Exception ex)
                {
                    Label1.Text = ex.Message + "-" + ex.StackTrace;

                }

          
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Datarollasc rasc = new Datarollasc();
            Datarolldesc rdes = new Datarolldesc();

            if (vary.count1 == 1)
            {
                vary.count1 = 0;
                data.Sort(rasc);
            }
            else if (vary.count1 == 0)
            {
                vary.count1 = 1;
                data.Sort(rdes);
            }
            GridView1.DataSource = data;
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Label1.Text = "click2";
            Datanameasc nasc = new Datanameasc();
            Datanamedesc ndsc = new Datanamedesc();

            if (vary.count2 == 1)
            {
                vary.count2 = 0;
                data.Sort(nasc);
            }
            else if (vary.count2 == 0)
            {
                vary.count2 = 1;
                data.Sort(ndsc);
            }
            GridView1.DataSource = data;
            GridView1.DataBind();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Label1.Text = "click3";
            Dataaddressasc addasc = new Dataaddressasc();
            Dataaddressdesc adddsc = new Dataaddressdesc();
            if (vary.count3 == 1)
            {
                vary.count3 = 0;
                data.Sort(addasc);
            }
            else if (vary.count3 == 0)
            {
                vary.count3 = 1;
                data.Sort(adddsc);
            }
            GridView1.DataSource = data;
            GridView1.DataBind();

        }
    }
}

Comments

Popular posts from this blog

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

How to add custom filter lookup field based on OptionSet field selection in Dynamics CRM.

go to top image