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