Click here to Skip to main content
15,886,810 members
Articles / Web Development / ASP.NET

GridView:Sorting & Paging Using Generics And Nullable Datatype Function Of C# GetValueOrDefault()

Rate me:
Please Sign up or sign in to vote.
4.31/5 (9 votes)
8 Dec 2008CPOL3 min read 63.4K   720   28  
This article demonstrate the Sorting mechanism for Nullable datatype when used with IList collection.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
/// <summary>
/// Summary description for Sample
/// </summary>
namespace SortingAndPaging
{

    public class Country
    {
        private int m_CountryID = int.MinValue;
        private string m_CountryName = string.Empty;
        private int? m_CountryTradeCode = null;

        public Country()
        {

        }
        public Country(int countryID)
        {
            m_CountryID = countryID;

        }
        public Country(int countryID, string countryName, int? countryTradeCode)
            : this(countryID)
        {
            m_CountryName = countryName;
            m_CountryTradeCode = countryTradeCode;
        }
        public int CountryID
        {
            get { return m_CountryID; }
            set { m_CountryID = value; }
        }
        public int? CountryTradeCode
        {
            get { return m_CountryTradeCode; }
            set { m_CountryTradeCode = value; }
        }
        public string CountryName
        {
            get { return m_CountryName; }
            set { m_CountryName = value; }
        }
        public int CompareTo(Country rhs, Country.CountryComparer.CountryComparisonType which)
        {
            switch (which)
            {
                case CountryComparer.CountryComparisonType.CountryID:
                    return this.m_CountryID.CompareTo(rhs.m_CountryID);
                case CountryComparer.CountryComparisonType.CountryName:
                    return this.m_CountryName.CompareTo(rhs.m_CountryName);
                case CountryComparer.CountryComparisonType.CountryTradeCode:
                    return this.m_CountryTradeCode.GetValueOrDefault().CompareTo(rhs.m_CountryTradeCode.GetValueOrDefault());

            }
            return 0;
        }
        public class CountryComparer : IComparer<Country>
        {
            public enum CountryComparisonType
            {
                CountryID,
                CountryName,
                CountryTradeCode,
                NULL
            }
            private CountryComparisonType _whichComparison;
            private Utility.SortOrder _sortDirection;
            public CountryComparisonType WhichComparison
            {
                get { return _whichComparison; }
                set { _whichComparison = value; }
            }
            public int Compare(Country lhs, Country rhs)
            {
                if (SortDirection == Utility.SortOrder.Asc)
                    return lhs.CompareTo(rhs, WhichComparison);
                else
                    return rhs.CompareTo(lhs, WhichComparison);
            }
            public bool Equals(Country lhs, Country rhs)
            {
                return this.Compare(lhs, rhs) == 0;
            }
            public int GetHashCode(Country e)
            {
                return e.GetHashCode();
            }
            public Utility.SortOrder SortDirection
            {
                get { return _sortDirection; }
                set { _sortDirection = value; }
            }


        }
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Technical Lead
Australia Australia
Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!
www.santoshpoojari.blogspot.com

Comments and Discussions