using System;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        RadGrid grid = new RadGrid();
        grid.ID = "RadGrid1";
        grid.Skin = "Vista";
        grid.Width = Unit.Percentage(100);
        grid.PageSize = 15;
        grid.AllowPaging = true;
        grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
        grid.AutoGenerateColumns = false;
        //Add Customers table  
        grid.MasterTableView.Width = Unit.Percentage(100);
        grid.MasterTableView.DataKeyNames = new string[] { "CustomerID" };
        GridBoundColumn boundColumn = new GridBoundColumn();
        boundColumn.DataField = "CustomerID";
        boundColumn.HeaderText = "Customer ID";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "CompanyName";
        boundColumn.HeaderText = "Company Name";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "ContactName";
        boundColumn.HeaderText = "Contact Name";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "ContactTitle";
        boundColumn.HeaderText = "Contact Title";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Address";
        boundColumn.HeaderText = "Address";
        grid.MasterTableView.Columns.Add(boundColumn);
        GridEditCommandColumn editColumn = new GridEditCommandColumn();
        grid.MasterTableView.Columns.Add(editColumn);
        grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
        grid.UpdateCommand += new GridCommandEventHandler(grid_UpdateCommand);
        grid.Visible = false;
        this.PlaceHolder1.Controls.Add(grid);
    }

    void grid_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        Response.Write("UpdateCommand fired for item with index: "+e.Item.ItemIndex);

    }

    void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        (sender as RadGrid).DataSource = GetDataTable("SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address FROM Customers");
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedItem.Text == "Show grid")
        {
            PlaceHolder1.FindControl("RadGrid1").Visible = true;
            (PlaceHolder1.FindControl("RadGrid1") as RadGrid).Rebind();
        }
        if (DropDownList1.SelectedItem.Text == "Hide grid")
        {
            PlaceHolder1.FindControl("RadGrid1").Visible = false;
        }
    }
    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;
    }


}