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

public partial class _Default : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{		
		RadGrid1.ItemCommand += RadGrid1_ItemCommand;
	}

	protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
	{
		RadGrid grid = (sender as RadGrid);
		if (e.CommandName == RadGrid.InitInsertCommandName)
		{
			grid.MasterTableView.ClearEditItems();
		}
		if (e.CommandName == RadGrid.EditCommandName)
		{
			e.Item.OwnerTableView.IsItemInserted = false;
		}
	}

	protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
	{
		string item = getItemName(e.Item.OwnerTableView.Name);
		string field = getFieldName(e.Item.OwnerTableView.Name);
		if (e.Exception != null)
		{
			e.KeepInEditMode = true;
			e.ExceptionHandled = true;
			DisplayMessage(item + " " + e.Item[field].Text + " cannot be updated. Reason: " + e.Exception.Message);
		}
		else
		{
			DisplayMessage(item + " " + e.Item[field].Text + " updated");
		}
	}

	protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
	{
		string item = getItemName(e.Item.OwnerTableView.Name);
		if (e.Exception != null)
		{
			e.ExceptionHandled = true;
			DisplayMessage(item + " cannot be inserted. Reason: " + e.Exception.Message);
		}
		else
		{
			DisplayMessage(item + " inserted");
		}
	}

	protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
	{
		string item = getItemName(e.Item.OwnerTableView.Name);
		string field = getFieldName(e.Item.OwnerTableView.Name);
		if (e.Exception != null)
		{
			e.ExceptionHandled = true;
			DisplayMessage(item + " " + e.Item[field].Text + " cannot be deleted. Reason: " + e.Exception.Message);
		}
		else
		{
			DisplayMessage(item + " " + e.Item[field].Text + " deleted");
		}
	}

	protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
	{
		if ("Orders".Equals(e.Item.OwnerTableView.Name))
		{
			GridDataItem parentItem = (GridDataItem)e.Item.OwnerTableView.ParentItem;
			SqlDataSource2.InsertParameters["CustomerID"].DefaultValue = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["CustomerID"].ToString();
		}
		else if ("Details".Equals(e.Item.OwnerTableView.Name))
		{
			GridDataItem parentItem = (GridDataItem)e.Item.OwnerTableView.ParentItem;
			SqlDataSource3.InsertParameters["OrderID"].DefaultValue = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["OrderID"].ToString();
		}
	}

	protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
	{
		GridEditableItem item = e.Item as GridEditableItem;
		if (item != null && item.IsInEditMode && item.ItemIndex != -1)
		{
			if (item.OwnerTableView.Name == ((RadGrid)sender).MasterTableView.Name)
			{
				(item.EditManager.GetColumnEditor("CustomerID").ContainerControl.Controls[0] as TextBox).Enabled = false;
			}
			else if (item.OwnerTableView.Name == "Details")
			{
				(item.EditManager.GetColumnEditor("ProductID").ContainerControl.Controls[0] as TextBox).Enabled = false;
			}
		}
	}

	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)));
	}


}