static void

Paged GridView: FakeDAL

The fake data access layer used by the paged GridView. There is a Load*Count method (count how many items, so we can work out page size- you can cache this) and a Load*ByPage with arguments for startrow, pagesize and sorting.

using System;
using System.Collections.Generic;
using System.Globalization;
 
/// <summary>
/// A fake data access layer
/// </summary>
/// <remarks>
/// For more detail see http://msdn2.microsoft.com/en-us/library/bb445504.aspx
/// for a tutorial on asp 2 paging and binding
/// </remarks>
[Serializable]
public class FakeDAL
{
    private readonly List<DatedItem> _items;
    public FakeDAL()
        : this(null)
    {
    }
    public FakeDAL(List<DatedItem> items)
    {
        if (items == null) items = CreateAllDatedItems();
        _items = items;
    }
 
    private static List<DatedItem> CreateAllDatedItems()
    {
        //create all the items
        List<DatedItem> items = new List<DatedItem>();
        int i = 100;
        CultureInfo[] allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
        foreach (CultureInfo ci in allCultures)
        {
            i = i + 10;
            DatedItem di = new DatedItem();
            di.Date = DateTime.Now.AddDays(-i);
            di.Id = i;
            di.Name = ci.EnglishName;
            items.Add(di);
        }
        return items;
    }
 
    public List<DatedItem> LoadAll()
    {
        return _items;
    }
 
    /// <summary>
    /// Generates some dummy data
    /// </summary>
    public List<DatedItem> LoadDatedItemsByPage(int startRow, int pageSize, string sortBy, bool sortAscending)
    {
        SortData(sortBy, sortAscending);
        //copy to an array
        int count = pageSize;
        if ((_items.Count - startRow) < count) count = _items.Count - startRow;
        DatedItem[] pagearray = new DatedItem[count];
        _items.CopyTo(startRow, pagearray, 0, count);
        //then create a list from array
        List<DatedItem> page = new List<DatedItem>(pagearray);
        return page;
    }
 
    public int LoadDatedItemsCount()
    {
        return _items.Count;
    }
 
    public DatedItem Find(int key)
    {
        return _items.Find(delegate(DatedItem d) { return (d.Id == key); });
    }
 
    public void Delete(int key)
    {
        DatedItem item= _items.Find(delegate(DatedItem d) { return (d.Id == key); });
        if (item!= null) _items.Remove(item);
    }
 
    private void SortData(string sortBy, bool sortAscending)
    {
        if (sortBy.Equals("Name", StringComparison.OrdinalIgnoreCase))
        {
            if (sortAscending)
                _items.Sort(delegate(DatedItem p1, DatedItem p2)
                { return p1.Name.CompareTo(p2.Name); });
            else
                _items.Sort(delegate(DatedItem p1, DatedItem p2)
                { return -p1.Name.CompareTo(p2.Name); });
        }
        else if (sortBy.Equals("Id", StringComparison.OrdinalIgnoreCase))
        {
            if (sortAscending)
                _items.Sort(delegate(DatedItem p1, DatedItem p2)
                { return p1.Id.CompareTo(p2.Id); });
            else
                _items.Sort(delegate(DatedItem p1, DatedItem p2)
                { return -p1.Id.CompareTo(p2.Id); });
        }
        else if (sortBy.Equals("Date", StringComparison.OrdinalIgnoreCase))
        {
            if (sortAscending)
                _items.Sort(delegate(DatedItem p1, DatedItem p2)
                { return p1.Date.CompareTo(p2.Date); });
            else
                _items.Sort(delegate(DatedItem p1, DatedItem p2)
                { return -p1.Date.CompareTo(p2.Date); });
        }
    }
}
 
//poco must be serializable so we can store it
[Serializable]
public class DatedItem
{
    private DateTime _date;
    private int _id;
    private string _name;
 
    public DateTime Date
    {
        get { return _date; }
        set { _date = value; }
    }
 
    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }
 
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}