Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

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

, 8 Dec 2008 CPOL
This article demonstrate the Sorting mechanism for Nullable datatype when used with IList collection.
Sorting_And_Paging.zip
Sorting And Paging
App_Code
Image
Asc.gif
Desc.gif
Thumbs.db
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)

Share

About the Author

santosh poojari
Technical Lead
India India
Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!
www.santoshpoojari.blogspot.com

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411019.1 | Last Updated 8 Dec 2008
Article Copyright 2008 by santosh poojari
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid