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 void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExpandCollapseCommandName)
        {
            (e.Item.FindControl("btnExpand") as ImageButton).Visible = !(e.Item.FindControl("btnExpand") as ImageButton).Visible;
            (e.Item.FindControl("btnCollapse") as ImageButton).Visible = !(e.Item.FindControl("btnCollapse") as ImageButton).Visible;
        }
        if (e.CommandName == "ExpandAll")
        {
            //Looping through each DataItem and making the "btnExpand" image button in the item visibility  to false and  "btnCollapse" visibility to true 
            foreach (GridDataItem GridDataItem in RadGrid1.MasterTableView.GetItems(new GridItemType[] { GridItemType.Item, GridItemType.AlternatingItem }))
            {
                ImageButton btnExpand = (ImageButton)GridDataItem.FindControl("btnExpand");
                btnExpand.Visible = false;
                ImageButton btnCollapse = (ImageButton)GridDataItem.FindControl("btnCollapse");
                btnCollapse.Visible = true;
            }
            //Exapanding the DataItem
            foreach (GridDataItem item in RadGrid1.Items)
            {
                item.Expanded = true;
            }
            //Hiding the CollapseAll link in the header to true and ExpandAll link in the header to false
            GridHeaderItem GridHeaderItem = e.Item as GridHeaderItem;
            LinkButton imgCollapseAll = (LinkButton)GridHeaderItem.FindControl("CollapseAll");
            imgCollapseAll.Visible = true;
            LinkButton imgExpandAll = (LinkButton)GridHeaderItem.FindControl("ExpandAll");
            imgExpandAll.Visible = false;
        }
        if (e.CommandName == "CollapseAll")
        {
            //Looping through each DataItem and making the "btnExpand" image button in the item visibility  to true and  "btnCollapse" visibility to false 
            foreach (GridDataItem GridDataItem in RadGrid1.MasterTableView.GetItems(new GridItemType[] { GridItemType.Item, GridItemType.AlternatingItem }))
            {
                ImageButton btnExpand = (ImageButton)GridDataItem.FindControl("btnExpand");
                btnExpand.Visible = true;
                ImageButton btnCollapse = (ImageButton)GridDataItem.FindControl("btnCollapse");
                btnCollapse.Visible = false;
            }
            //Collapsing the DataItem
            foreach (GridDataItem item in RadGrid1.Items)
            {
                item.Expanded = false;
            }
            //Hiding the CollapseAll link in the header to false and ExpandAll link in the header to true
            GridHeaderItem GridHeaderItem = e.Item as GridHeaderItem;
            LinkButton imgCollapseAll = (LinkButton)GridHeaderItem.FindControl("CollapseAll");
            imgCollapseAll.Visible = false;
            LinkButton imgExpandAll = (LinkButton)GridHeaderItem.FindControl("ExpandAll");
            imgExpandAll.Visible = true;
        }
    }
}

