Tuesday, June 30, 2009

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

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.


  • See full detail: http://www.codeproject.com/KB/aspnet/GridSortingPaging.aspx

    No comments: