using System;
using System.Data;
using System.Collections;
using System.Web.SessionState;
using Telerik.Web.UI;
using System.Collections.Generic;
using Telerik.Web.Examples.Grid;
using System.Data.OleDb;
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();
        if (!export)
        {
            RadGrid1.DataSource = MyBusinessObjectCollection1.Select(startRowIndex, maximumRows);
        }
        else
        {
            RadGrid1.DataSource = MyBusinessObjectCollection1.Select(0, MyBusinessObjectCollection1.SelectCount());
        }
    }

    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;
    }
    bool export = false;
    protected void Button1_Click(object sender, EventArgs e)
    {
        export = true;
        RadGrid1.ExportSettings.ExportOnlyData = true;
        RadGrid1.ExportSettings.IgnorePaging = true;
        RadGrid1.ExportSettings.OpenInNewWindow = true;
        RadGrid1.MasterTableView.ExportToExcel(); 

    }
}
       