Click here to Skip to main content
13,006,288 members (57,539 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 8 Dec 2008

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

, 8 Dec 2008
Rate this:
Please Sign up or sign in to vote.
This article demonstrate the Sorting mechanism for Nullable datatype when used with IList collection.


The idea behind writing this article is to overcome the problem related to sorting of collection having null values in it. Recently I was working with GridView control and I was using generic collection list as a data source to it. While resolving this problem alone I developed a good piece of functionality which I do feel will help most of you in your development activity.

Features Available

The feature incorporated in the GridView are as follows:

  • Used C#'s Generics
  • Used Nullable Datatype
  • Used Generics Collection IList To Bind GridView. 
  • Sorting Of Generics Collection IList Using IComparer Interface .
  • Applied Nullable Datatype function GetValueOrDefault for Sort Operation On Nullable Datafield.
  • Fully Tested Concurrent Sorting And Paging Operation In GridView.

    Problem Statement

    I had nullable value field data type in my database table. So I used nullable datatype of C# in my code. Doing so I encountered sorting failure problem with IComparer. After some research and development I came up with the solution. I used GetValueOrDefault() to resolve this. How I applied this to my solution is demonstrated below.

    Consider Database table schema as:
    Column DataType Nullable Values
    CountryID Int No
    CountryName String No
    CountryTradeCode Int Yes


    Implement IComparer:Business Entity Class Country

    In below class we have three field as discussed. In this we have m_CountryTradeCode as nullable data type. I was facing problem sorting nullable data type. So I got a workaround for this as shown here.

     case CountryComparer.CountryComparisonType.CountryTradeCode:
    return this.m_CountryTradeCode.GetValueOrDefault().CompareTo(rhs.m_CountryTradeCode.GetValueOrDefault());
    An important aspect to know about IComparer is that it operates on a complete Class Object and it knows about the object elements while IComparable operates only on elements of the object. The complete class is given below.
    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.
                return 0;
            public class CountryComparer : IComparer< Country >
                public enum CountryComparisonType
                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);
                        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; }

    Populate Collection List With Country Object

    One can get this data from database using datareader and can populate this collection list as given below.For ready to go solution I have not used database and have hardcoded the values for understanding purpose. If you notice there is null fields in the objects that is been added as record in Collection List.The important challenge here is to sort this collection list having null value field. The nullable type is only applied to value type. As null reference is not assigned to value type,C# nullable datatype provide this feature.For eg. int a=null; is not allowed in C#. This is overcome by nullable datatype and the above example can be re written for nullable type as int? a = null;

    private IList<Country> GetCountryList()
               IList < Country > countryList = new List < Country >();
               countryList.Add(new Country(1, "United States Of America", 100001));
               countryList.Add(new Country(2, "Africa", null));
               countryList.Add(new Country(3, "India", null));
               countryList.Add(new Country(4, "Singapore", 100004));
               countryList.Add(new Country(5, "Newzealand", 100005));
               countryList.Add(new Country(6, "United Kingdom", 100006));
               countryList.Add(new Country(7, "Australia", 100007));
               countryList.Add(new Country(8, "China", 100008));
               countryList.Add(new Country(9, "Malaysia", null));
               countryList.Add(new Country(10, "Germany", 100011));
               countryList.Add(new Country(11, "France", 100009));
               countryList.Add(new Country(12, "United States Of Soviet Russia", null));
               return countryList;

    C# Generics Method:Bind GridView Control

    private void BindGrid < T >(T list)
         gridCountry.DataSource = list;

    Sort Event Logic

    private void SortCountryList(Country.CountryComparer.CountryComparisonType sortExpression)
                List < Country > countryList = (List < Country >)GetCountryList();
                Country.CountryComparer countryComparer = new Country.CountryComparer();
                if (Country.CountryComparer.CountryComparisonType.NULL != sortExpression)
                    if (sortExpression == SortColumn)
                        if (SortDirection == Utility.SortOrder.Asc)
                            SortDirection = Utility.SortOrder.Desc;
                            SortDirection = Utility.SortOrder.Asc;
                        SortDirection = Utility.SortOrder.Asc;
                    SortColumn = sortExpression;
                countryComparer.WhichComparison = SortColumn;
                countryComparer.SortDirection = SortDirection;
                CountryDataSource = countryList;

    Concurrent paging And Sorting Operation On Grid

    Contrary to what the title suggests,paging and sort operation here is not happening at the same time.The flow of the program suggests that the sorted order of a given column in a grid has to be maintained when paging operation is done.That means paging is carried out on sorted List.For this I used Viewstate to maintain the current operation.

          private Country.CountryComparer.CountryComparisonType SortColumn
        get { return (Country.CountryComparer.CountryComparisonType)ViewState["SORT_EXP"]; }
        set { ViewState["SORT_EXP"] = value; }
    private Utility.SortOrder SortDirection
        get { return (Utility.SortOrder)ViewState["SORT_ORDER"]; }
        set { ViewState["SORT_ORDER"] = value; }

    Initially the list is loaded with current sort order and sort direction.

    protected void Page_Load(object sender, EventArgs e)
                  if (IsPostBack == false)
                      SortColumn = Country.CountryComparer.CountryComparisonType.CountryID;
                      SortDirection = Utility.SortOrder.Asc;
              catch (Exception ex)
                  throw ex;
          private void LoadCountryList()

    This is called on page change event on sorted List.

    private void PagingCountryList()

    One can download the code and can check the code for more understanding and usage purpose.


    Hope I met the expectations of the reader and also please vote for this article ,this will help me to understand the quality I put in. Any suggestion and advice are most welcome. Looking forward for your feedback.




    First Cut-Initial Draft: 8-Dec-2008

  • License

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


    About the Author

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

    You may also be interested in...


    Comments and Discussions

    GeneralNice work Pin
    stevetcp11-Jan-09 18:58
    memberstevetcp11-Jan-09 18:58 
    GeneralRe: Nice work Pin
    santosh poojari14-Jan-09 23:23
    membersantosh poojari14-Jan-09 23:23 
    General[Message Deleted] Pin
    Vladimir_V8-Dec-08 11:36
    memberVladimir_V8-Dec-08 11:36 
    GeneralRe: My vote of 1 Pin
    BAIJUMAX8-Dec-08 17:12
    memberBAIJUMAX8-Dec-08 17:12 
    GeneralImage conveys lot more than words Pin
    Gautam Sharma8-Dec-08 18:11
    memberGautam Sharma8-Dec-08 18:11 
    GeneralMy vote of 1 Pin
    CPAV8-Dec-08 10:20
    memberCPAV8-Dec-08 10:20 
    play Cricket pls
    team lead? LOL
    GeneralDude, Africa is not a country Pin
    binarycheese8-Dec-08 3:24
    memberbinarycheese8-Dec-08 3:24 
    GeneralRe: Dude, Africa is not a country Pin
    thund3rstruck8-Dec-08 15:28
    memberthund3rstruck8-Dec-08 15:28 
    GeneralRe: Dude, Africa is not a country Pin
    dougnlamb10-Jul-09 9:39
    memberdougnlamb10-Jul-09 9:39 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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