Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET
Article

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.2K   720   28   9
This article demonstrate the Sorting mechanism for Nullable datatype when used with IList collection.

Introduction

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 asp.net 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.
    C#
    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; }
                }
            }
        }

    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;

    C#
    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

    C#
    private void BindGrid < T >(T list)
     {
         gridCountry.DataSource = list;
         gridCountry.DataBind();
     }
    

    Sort Event Logic

    C#
    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;
                        }
                        else
                        {
                            SortDirection = Utility.SortOrder.Asc;
                        }
                    }
                    else
                    {
                        SortDirection = Utility.SortOrder.Asc;
                    }
                    SortColumn = sortExpression;
                }
                countryComparer.WhichComparison = SortColumn;
                countryComparer.SortDirection = SortDirection;
                countryList.Sort(countryComparer);
                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.

    C#
          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.

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

    This is called on page change event on sorted List.

    C#
    private void PagingCountryList()
          {
              SortCountryList(Country.CountryComparer.CountryComparisonType.NULL);
          }
    

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

    Conclusion

    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.

    References

    Icomparer:Reference: http://www.ondotnet.com/pub/a/dotnet/excerpt/progcsharp4_ch09-04/index.html?page=5

    History

    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)


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

    Comments and Discussions

     
    GeneralNice work Pin
    stevetcp11-Jan-09 18:58
    stevetcp11-Jan-09 18:58 
    GeneralRe: Nice work Pin
    santosh poojari14-Jan-09 23:23
    santosh poojari14-Jan-09 23:23 
    General[Message Deleted] Pin
    Vladimir_V8-Dec-08 11:36
    Vladimir_V8-Dec-08 11:36 
    GeneralRe: My vote of 1 Pin
    BAIJUMAX8-Dec-08 17:12
    professionalBAIJUMAX8-Dec-08 17:12 
    GeneralImage conveys lot more than words Pin
    Gautam Sharma8-Dec-08 18:11
    Gautam Sharma8-Dec-08 18:11 
    GeneralMy vote of 1 Pin
    CPAV8-Dec-08 10:20
    CPAV8-Dec-08 10:20 
    GeneralDude, Africa is not a country Pin
    Tawani Anyangwe8-Dec-08 3:24
    Tawani Anyangwe8-Dec-08 3:24 
    GeneralRe: Dude, Africa is not a country Pin
    thund3rstruck8-Dec-08 15:28
    thund3rstruck8-Dec-08 15:28 
    GeneralRe: Dude, Africa is not a country Pin
    dougnlamb10-Jul-09 9:39
    dougnlamb10-Jul-09 9:39 
    Neither is United States of Soviet Russia.

    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.