using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Telerik.Web.UI;
using System.Data;

public partial class Default : System.Web.UI.Page
{
    bool isExport = false;
    protected void btn_clicked(object o, EventArgs e)
    {
        isExport = true;
        RadGrid1.Rebind();
        RadGrid1.MasterTableView.ExportToExcel();
        

        
    }



    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        RadGrid1.MasterTableView.Items[0].Expanded = true;
        
        if (isExport)
        {
            foreach (GridItem item in RadGrid1.MasterTableView.Items)
            {
                if (item.Expanded)
                {
                    GridDataItem dItem = item as GridDataItem;
                    GridItem[] items = dItem.ChildItem.NestedTableViews[0].GetItems(GridItemType.NoRecordsItem);
                    if (items.Length > 0)
                    {
                        items[0].Visible = false;
                       
                    }
                }
            }
           
        }
    }
  


    private Hashtable _ordersExpandedState;

    private Hashtable ExpandedStates
    {
        get
        {
            if (this._ordersExpandedState == null)
            {
                _ordersExpandedState = this.Session["_ordersExpandedState"] as Hashtable;
                if (_ordersExpandedState == null)
                {
                    _ordersExpandedState = new Hashtable();
                    this.Session["_ordersExpandedState"] = _ordersExpandedState;
                }
            }

            return this._ordersExpandedState;
        }
    }

    


    private void Page_Init(object sender, EventArgs e)
    {

        if (IsPostBack)
        {
            this._ordersExpandedState = null;
            this.Session["_ordersExpandedState"] = null;
          
        }
    }

   

   

    private String getItemName(string tableName)
    {
        switch (tableName)
        {
            case ("Customers"):
                {
                    return "Customer";
                }
            case ("Orders"):
                {
                    return "Order";
                }
            case ("Details"):
                {
                    return "Details for order";
                }
            default: return "";
        }
    }

    private String getFieldName(string tableName)
    {
        switch (tableName)
        {
            case ("Customers"):
                {
                    return "CustomerID";
                }
            case ("Orders"):
                {
                    return "OrderID";
                }
            case ("Details"):
                {
                    return "OrderID";
                }
            default: return "";
        }
    }

    private void DisplayMessage(string text)
    {
        RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
    }
}