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

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Assembly.GetAssembly(typeof(ScriptManager)).FullName.IndexOf("3.5") != -1)
        {
            RadGrid1.MasterTableView.FilterExpression = @"it[""ReportsTo""] = Convert.DBNull";
        }
        else
        {
            RadGrid1.MasterTableView.FilterExpression = "ReportsTo IS NULL";
        }

    }

    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = Employees;
    }

    public DataTable GetDataTable(string query)
    {
        String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(ConnString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(query, conn);

        DataTable myDataTable = new DataTable();

        conn.Open();
        try
        {
            adapter.Fill(myDataTable);
        }
        finally
        {
            conn.Close();
        }

        return myDataTable;
    }

    private DataTable Employees
    {
        get
        {
            object obj = this.Session["Employees"];
            if ((!(obj == null)))
            {
                return ((DataTable)(obj));
            }
            DataTable myDataTable = new DataTable();
            myDataTable = GetDataTable("SELECT * FROM Employees");
            this.Session["Employees"] = myDataTable;
            return myDataTable;
        }
    }

    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "PerformInsert") 
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;

            //Create new row in the DataSource
            DataRow newRow = this.Employees.NewRow();

            //Insert new values
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);

            ////make sure that unique primary key value is generated for the inserted row 
            newValues["EmployeeID"] = (int)this.Employees.Rows[this.Employees.Rows.Count - 1]["EmployeeID"] + 1;
            try
            {
                foreach (DictionaryEntry entry in newValues)
                {
                    newRow[(string)entry.Key] = entry.Value;
                }
                this.Employees.Rows.Add(newRow);
                this.Employees.AcceptChanges();
            }
            catch (Exception ex)
            {
                Label lblError = new Label();
                lblError.Text = "Unable to insert Employees. Reason: " + ex.Message;
                lblError.ForeColor = System.Drawing.Color.Red;
                RadGrid1.Controls.Add(lblError);

                e.Canceled = true;
            }
        }
    }

    public void Page_PreRenderComplete(object sender, EventArgs e)
    {
        foreach (GridDataItem item in RadGrid1.Items)
        {
            //You can expand a corresponding item depending on a Item field value.
            if (item["FirstName"].Text == "Andrew")
            {
                item.Expanded = true;
            }
            //You can disable or enable build-in expand button depending on some item field value
            if (item["FirstName"].Text == "Janet" && item["ExpandColumn"] != null)
            {
                Button button = (Button)item["ExpandColumn"].Controls[0];
                button.Attributes.Add("disabled", "true");
                button.ToolTip = "Disabled Button";
            }
        }
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
    }
}
