Click here to Skip to main content
15,896,063 members
Articles / Web Development / ASP.NET

Create Your Own GridView Control - the ALT.Net's Way

Rate me:
Please Sign up or sign in to vote.
4.83/5 (10 votes)
26 Mar 2012CPOL9 min read 74.8K   4.5K   37  
A GridView user control that you can modify and customise to produce the specific grid you want.
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;

namespace GenericGridViewHelper
{
    public class DataSourceHelper
    {
        public static void PrepareDataSource(
            GridView gvGeneric,
            StateBag viewState,
            ref IEnumerable DataSource,
            ref int _totalRowCount,
            Func<GridViewSortEventArgs, IEnumerable> sorting)
        {
            var column = viewState[Const.SortExpression] as string;
            SortDirection sortDirection = SortDirection.Ascending;
            if (viewState[Const.SortDirection] != null)
            {
                sortDirection = (SortDirection)viewState[Const.SortDirection];
            }

            var temp = DataSource.Cast<object>();
            _totalRowCount = temp.Count();

            if (column != null)
            {
                var source = SortingHelper.GetSortedObjects(new GridViewSortEventArgs(column, sortDirection), temp, sorting);
                DataSource = source;
            }
            else if (DataSource.GetType().Name != Const.ListTypeName)
            {
                DataSource = temp.ToList();
            }

            gvGeneric.DataSource = DataSource;
        }

        public static void SelectDataSourceAndBind(CompositeDataBoundControl gvGeneric, IEnumerable sortedDataSource, IEnumerable orginalDataSource, GridViewSortEventArgs e)
        {
            if (sortedDataSource != null)
            {
                if (sortedDataSource.GetType().Name != Const.ListTypeName)
                {
                    gvGeneric.DataSource = sortedDataSource.Cast<object>().ToList();
                }
                else
                {
                    gvGeneric.DataSource = sortedDataSource;
                }
            }
            else
            {
                gvGeneric.DataSource = SortingHelper.GetSortedObjects(e, orginalDataSource.Cast<object>(), null);
            }

            gvGeneric.DataBind();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Team Leader XYZ Studios Ltd
China China
I am an independent IT contractor who is passionate about life and Microsoft technology stack. I have been programming since 2001. Besides coding, I also enjoy diving, swimming, rock climbing and travelling. I swim 2 miles a day to keep my morale and energy level consistently high.

Comments and Discussions