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

public partial class _Default : System.Web.UI.Page
{
    RadGrid RadGrid1;

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        PopulateGrid1OnPageInit();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            PopulateGrid2OnPageLoad();

        }
    }
    protected void PopulateGrid1OnPageInit()
    {
        RadGrid1 = new RadGrid();
        RadGrid1.ID = "RadGrid1";
        RadGrid1.MasterTableView.AutoGenerateColumns = false;
        RadGrid1.MasterTableView.AllowPaging = true;
        RadGrid1.PagerStyle.Mode = GridPagerMode.NumericPages;
        RadGrid1.AllowSorting = true;

        RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);

        GridBoundColumn boundColumn = new GridBoundColumn();
        boundColumn.HeaderText = "ContactName";
        boundColumn.DataField = "ContactName";
        boundColumn.UniqueName = "ContactName";
        RadGrid1.MasterTableView.Columns.Add(boundColumn);

        boundColumn = new GridBoundColumn();
        boundColumn.HeaderText = "ContactTitle";
        boundColumn.DataField = "ContactTitle";
        boundColumn.UniqueName = "ContactTitle";
        RadGrid1.MasterTableView.Columns.Add(boundColumn);


        boundColumn = new GridBoundColumn();
        boundColumn.HeaderText = "Address";
        boundColumn.DataField = "Address";
        boundColumn.UniqueName = "Address";
        RadGrid1.MasterTableView.Columns.Add(boundColumn);

        GridEditCommandColumn editColumn = new GridEditCommandColumn();
        editColumn.HeaderText = "Edit column";
        editColumn.UniqueName = "EditCommandColumn";
        RadGrid1.MasterTableView.Columns.Add(editColumn);

        GridButtonColumn buttonColumn = new GridButtonColumn();
        buttonColumn.CommandName = "Delete";
        buttonColumn.ButtonType = GridButtonColumnType.PushButton;
        buttonColumn.Text = "delete row";
        buttonColumn.ConfirmText = "Are you sure you would like to remove this user from the role?";
        buttonColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        RadGrid1.MasterTableView.Columns.Add(buttonColumn);

        PlaceHolder1.Controls.Add(RadGrid1);

    }
    void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = GetData("SELECT ContactName, ContactTitle, Address FROM [Customers]");
    }
    protected void PopulateGrid2OnPageLoad()
    {
        GridBoundColumn boundColumn = new GridBoundColumn();
        RadGrid2.MasterTableView.Columns.Add(boundColumn);
        boundColumn.HeaderText = "ContactName";
        boundColumn.DataField = "ContactName";
        boundColumn.UniqueName = "ContactName";

        boundColumn = new GridBoundColumn();
        RadGrid2.MasterTableView.Columns.Add(boundColumn);
        boundColumn.HeaderText = "ContactTitle";
        boundColumn.DataField = "ContactTitle";
        boundColumn.UniqueName = "ContactTitle";


        boundColumn = new GridBoundColumn();
        RadGrid2.MasterTableView.Columns.Add(boundColumn);
        boundColumn.HeaderText = "Address";
        boundColumn.DataField = "Address";
        boundColumn.UniqueName = "Address";


        GridEditCommandColumn editColumn = new GridEditCommandColumn();
        RadGrid2.MasterTableView.Columns.Add(editColumn);
        editColumn.HeaderText = "Edit column";
        editColumn.UniqueName = "EditCommandColumn";

        GridButtonColumn buttonColumn = new GridButtonColumn();
        RadGrid2.MasterTableView.Columns.Add(buttonColumn);
        buttonColumn.CommandName = "Delete";
        buttonColumn.ButtonType = GridButtonColumnType.PushButton;
        buttonColumn.Text = "delete row";
        buttonColumn.ConfirmText = "Are you sure you would like to remove this user from the role?";
        buttonColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
    }
    protected void RadGrid2_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        RadGrid2.DataSource = GetData("SELECT ContactName, ContactTitle, Address FROM [Customers]");
    }
    private DataTable GetData(String queryString)
    {
        String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        SqlConnection MySqlConnection = new SqlConnection(ConnString);
        SqlDataAdapter MySqlDataAdapter = new SqlDataAdapter();
        MySqlDataAdapter.SelectCommand = new SqlCommand(queryString, MySqlConnection);

        DataTable myDataTable = new DataTable();
        MySqlConnection.Open();
        try
        {
            MySqlDataAdapter.Fill(myDataTable);
        }
        finally
        {
            MySqlConnection.Close();
        }

        return myDataTable;
    }
}
