using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;

public partial class Default : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
        RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            RadGrid1.Rebind(); 
            CollapseAll();
        }
    }

    private void CollapseAll()
    {
        foreach (GridItem item in RadGrid1.MasterTableView.Controls[0].Controls)
        {
            if (item is GridGroupHeaderItem)
            {
                item.Expanded = false;
            }
        }
    }

    void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExpandCollapseCommandName)
        {
            GridGroupHeaderItem groupHeaderItem = (GridGroupHeaderItem)e.Item;

            e.Canceled = true;

            CollapseAll();

            // When "Expand" is clicked, the expanded state is set to false. Therefore, the division from this group header  
            // needs to be read. When "Collapse" is clicked, the state is assigned "true"  
            if (!groupHeaderItem.Expanded)
            {
                //string currentDivision = this.GetDivisionFromGroupHeader(groupHeaderItem);
                //new ObjectLifecycleHelper(this.Page, this.ViewState).SetCurrentSelectedGridDivision(currentDivision);

                groupHeaderItem.Expanded = true;
            }

            if (!RadGrid1.AllowPaging)
            {
                RadGrid1.AllowPaging = true;
                RadGrid1.PageSize = 15;
            }
            // rebind in each case, because otherwise the Expanded/Collapsed state is not changed by the view  
            RadGrid1.Rebind();
        }
    }

    void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID");
        table.Columns.Add("Item");
        table.Columns.Add("Date", typeof(DateTime));
        int count = 0;
        for (int i = 0; i < 150; i++)
        {
            if (i % 30 == 0)
            {
                count++;
            }
            table.Rows.Add(i, "Item" + i.ToString(), DateTime.Now.AddDays(count));
        }
        RadGrid1.DataSource = table;
    }
}
