Sep 1, 2007

Custom Sorting Paging for GridView

ObjectDataSource can do sorting paging without writing code in the aspx. The idea of using object data source is to move the code out of page.
public class MyCustomer
{
   public static int  SelectCount()
   {
     return 20;
   }

   public static DataTable SelectData()
   {
     return SelectData(null);
   }


   public static DataTable SelectData(string sortExpression)
   {
     return SelectData(0, 0);
   }

   public static DataTable SelectData(int maximumRows, int startRowIndex)
   {
      return SelectData(null, maximumRows, startRowIndex);
   }

   public static DataTable SelectData(string sortExpression, int maximumRows, int startRowIndex)
   {
     DataTable dt = new DataTable();
     dt.Columns.Add("id", typeof(int));
     dt.Columns.Add("name", typeof(string));
     int upbound = SelectCount();
     if (maximumRows != 0)
     {
       if (upbound > maximumRows)
       {
         upbound = maximumRows;
       }
     }

     for (int i = 0; i < upbound; i++)
     {
        int current = i + startRowIndex + 1;
        dt.Rows.Add(new object[] { current, current.ToString() });
     }
     return dt;
  }
}
Because GridView does not have VirtualItemCount(DataGrid has), manually binding to a datasource is very difficult. This aritcal provide a solution the implement the VirtualItemCount.