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

public partial class GridAutomaticOperations : System.Web.UI.Page
{
    String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
    SqlDataAdapter adapter = new SqlDataAdapter();
    public SqlCommand SqlCommand = new SqlCommand();
    protected void Page_Load(object sender, EventArgs e)
    {
        RadGrid1.UpdateCommand += new GridCommandEventHandler(RadGrid1_UpdateCommand);
        RadGrid1.InsertCommand += new GridCommandEventHandler(RadGrid1_InsertCommand);
    }

    protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
        GridDataItem dataItem = e.Item as GridDataItem;
        RadComboBox combo = dataItem.FindControl("RadComboBox1") as RadComboBox;
        SqlConnection conn = new SqlConnection(ConnString);
        try
        {
            conn.Open();
            string insertQuery = "INSERT INTO  Employees(LastName, FirstName,Title,Address,City, TitleOfCourtesy) VALUES('" + "Telerik" + "','" + "Telerik" + "','" + "Telerik" + "','" + "Telerik" + "','" + "Telerik" + "','" + combo.SelectedValue + "')";
            SqlCommand.CommandText = insertQuery;
            SqlCommand.Connection = conn;
            SqlCommand.ExecuteNonQuery();
        }
        finally
        {
            conn.Close();
        }
    }

    protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridDataItem dataItem = e.Item as GridDataItem;
        string employeeID = (e.Item as GridDataItem).GetDataKeyValue("EmployeeID").ToString();
        RadComboBox combo = dataItem.FindControl("RadComboBox1") as RadComboBox;
        SqlConnection conn = new SqlConnection(ConnString);
        try
        {
            conn.Open();
            string updateQuery = "UPDATE Employees set TitleOfCourtesy='" + combo.SelectedValue + "' WHERE EmployeeID= '" + employeeID + "'";
            SqlCommand.CommandText = updateQuery;
            SqlCommand.Connection = conn;
            SqlCommand.ExecuteNonQuery();
        }
        finally
        {
            conn.Close();
        }
    }

    protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        GridDataItem dataItem = (sender as RadComboBox).NamingContainer as GridDataItem;
        if (RadGrid1.MasterTableView.IsItemInserted)
        {
            dataItem.FireCommandEvent("PerformInsert", string.Empty);
        }
        else
        {
            dataItem.FireCommandEvent("Update", string.Empty);
        }
    }

    public DataTable GetDataTable(string query)
    {
        SqlConnection conn = new SqlConnection(ConnString);
        adapter.SelectCommand = new SqlCommand(query, conn);

        DataTable myDataTable = new DataTable();

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

        return myDataTable;
    }

    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = GetDataTable("SELECT [EmployeeID], [TitleOfCourtesy] FROM [Employees]");
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.OwnerTableView.IsItemInserted)
        {

        }
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            LinkButton updateButton = dataItem.FindControl("AutoGeneratedUpdateButton") as LinkButton;
            if (updateButton != null)
            {
                updateButton.Visible = false;
            }
            LinkButton insertButton = dataItem.FindControl("AutoGeneratedPerformInsertButton") as LinkButton;
            if (insertButton != null)
            {
                insertButton.Visible = false;
            }
        }
    }
}