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

namespace MvcApplication1.Views.Home
{
    public partial class Index : ViewPage
    {
        DataClasses1DataContext context = new DataClasses1DataContext();

        protected void MyGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.UpdateCommandName)
            {
                e.Canceled = true;

                int EmployeeID = (int)((GridDataItem)e.Item).GetDataKeyValue("EmployeeID");

                LargeEmployee employee = context.LargeEmployees.Where(n => n.EmployeeID == EmployeeID).FirstOrDefault();
                
                BindingHelperExtensions.UpdateFrom(employee, Request.Form);
                
                context.SubmitChanges();

                e.Item.OwnerTableView.Rebind();
            }
            else if (e.CommandName == "Insert")
            {
                LargeEmployee employee = new LargeEmployee();

                BindingHelperExtensions.UpdateFrom(employee, Request.Form);

                context.LargeEmployees.InsertOnSubmit(employee);

                context.SubmitChanges();

                e.Item.OwnerTableView.Rebind();
            }
        }
    }
}
