using System;
using Telerik.Web.UI;
using Telerik.Web.Examples.Grid;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        MyBusinessObjectCollection MyBusinessObjectCollection1 = new MyBusinessObjectCollection();
        int startRowIndex = (ShouldApplySortFilterOrGroup()) ?
            0 : RadGrid1.CurrentPageIndex * RadGrid1.PageSize;

        int maximumRows = (ShouldApplySortFilterOrGroup()) ?
            MyBusinessObjectCollection1.SelectCount() : RadGrid1.PageSize;

        RadGrid1.AllowCustomPaging = !ShouldApplySortFilterOrGroup();

        RadGrid1.DataSource = MyBusinessObjectCollection1.Select(startRowIndex, maximumRows);
    }

    bool isGrouping = false;
    protected void RadGrid1_GroupsChanging(object source, GridGroupsChangingEventArgs e)
    {
        isGrouping = true;
        if (e.Action == GridGroupsChangingAction.Ungroup && RadGrid1.CurrentPageIndex > 0)
        {
            isGrouping = false;
        }
    }

    public bool ShouldApplySortFilterOrGroup()
    {
        return RadGrid1.MasterTableView.FilterExpression != "" ||
            (RadGrid1.MasterTableView.GroupByExpressions.Count > 0 || isGrouping) ||
            RadGrid1.MasterTableView.SortExpressions.Count > 0;
    }

    protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
    {
        if (e.Column.IsBoundToFieldName("Date"))
        {
            (e.Column as GridDateTimeColumn).DataFormatString = "{0:D}";
        }
        else if (e.Column.IsBoundToFieldName("UnitPrice"))
        {
            (e.Column as GridNumericColumn).DataFormatString = "{0:C}";
        }
        if (e.Column is GridBoundColumn)
        {
            (e.Column as GridBoundColumn).FilterControlWidth = Unit.Pixel(100);
        }
    }

    
}