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

public partial class RadGridWithMenu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (RadGrid1.EditIndexes.Count > 0 || RadGrid1.MasterTableView.IsItemInserted)
        {
            GridColumn EditCommandColumn = RadGrid1.MasterTableView.GetColumn("EditCommandColumn") as GridColumn;
            if (EditCommandColumn != null)
                EditCommandColumn.Visible = true;
        }
        else
        {
            GridColumn EditCommandColumn = RadGrid1.MasterTableView.GetColumn("EditCommandColumn") as GridColumn;
            if (EditCommandColumn != null)
                EditCommandColumn.Visible = false;
        }
    }
    protected void RadMenu1_ItemClick(object sender, RadMenuEventArgs e)
    {
        int radGridClickedRowIndex;

        radGridClickedRowIndex = Convert.ToInt32(Request.Form["radGridClickedRowIndex"]);

        switch (e.Item.Text)
        {
            case "Edit":
                if (RadGrid1.Items[radGridClickedRowIndex] != null)
                    RadGrid1.Items[radGridClickedRowIndex].Edit = true;
                RadGrid1.Rebind();
                break;
            case "Add":
                RadGrid1.MasterTableView.IsItemInserted = true;
                RadGrid1.Rebind();
                break;
            case "Delete":
                if (RadGrid1.Items[radGridClickedRowIndex] != null)
                    RadGrid1.MasterTableView.PerformDelete(RadGrid1.Items[radGridClickedRowIndex]);
                break;
        }
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            Button templateColumnButton = dataItem["TemplateButtonColumn1"].Controls[1] as Button;
            if (templateColumnButton != null)
                templateColumnButton.OnClientClick = "RowContextMenu(this, event, " + dataItem.ItemIndexHierarchical + ");return false;";
        }
    }
}