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;

public partial class _Default : System.Web.UI.Page
{
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridHeaderItem)
        {
            foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
            {
                if (!column.Visible)
                    continue;

                ((GridHeaderItem)e.Item)[column.UniqueName].Controls.Clear();

                Label Label1 = new Label();
                Label1.Text = column.HeaderText;
                Label1.Style["float"] = "left";
                ((GridHeaderItem)e.Item)[column.UniqueName].Controls.Add(Label1);

                RadMenu RadMenu1 = new RadMenu();
                RadMenu1.Skin = "Vista";
                RadMenu1.Style["border"] = "0px;";
                RadMenu1.Style["float"] = "right";
                RadMenu1.Style["margin-right"] = "10px";
                RadMenu1.Items.Add(new RadMenuItem(""));
                RadMenu1.Items[0].ImageUrl = "~/image.gif";
                RadMenu1.Items[0].PostBack = false;

                RadMenuItem RadMenuItem1 = new RadMenuItem("Sort Ascending");
                RadMenuItem1.Value = column.UniqueName;
                RadMenu1.Items[0].Items.Add(RadMenuItem1);

                RadMenuItem1 = new RadMenuItem("Sort Descending");
                RadMenuItem1.Value = column.UniqueName;
                RadMenu1.Items[0].Items.Add(RadMenuItem1);

                RadMenuItem separator = new RadMenuItem();
                separator.IsSeparator = true;
                RadMenu1.Items[0].Items.Add(separator);

                RadMenuItem1 = new RadMenuItem("Filter");
                RadMenuItem1.Value = column.UniqueName;
                RadMenu1.Items[0].Items.Add(RadMenuItem1);

                RadMenuItem RadMenuSubItem1 = new RadMenuItem();
                RadMenuSubItem1.ItemTemplate = new MyTemplate();
                RadMenuSubItem1.Width = Unit.Pixel(135);
                RadMenuSubItem1.Value = column.UniqueName;
                RadMenuItem1.Items.Add(RadMenuSubItem1);

                RadMenu1.ItemClick += new RadMenuEventHandler(RadMenu1_ItemClick);

                ((GridHeaderItem)e.Item)[column.UniqueName].Controls.Add(RadMenu1);
            }
        }
    }

    void RadMenu1_ItemClick(object sender, RadMenuEventArgs e)
    {
        if (e.Item.Text.IndexOf("Sort") != -1)
        {
            string expression = String.Format("{0} {1}", e.Item.Value, (e.Item.Text.Split(' ')[1] == "Descending") ? "DESC" : "");
            RadGrid1.MasterTableView.SortExpressions.AddSortExpression(expression);
            RadGrid1.Rebind();
        }
        else if (e.Item.Text == "Filter")
        {
            RadGrid1.MasterTableView.FilterExpression = "";
            RadGrid1.Rebind();
        }
    }

    public class MyTemplate : ITemplate
    {

        #region ITemplate Members

        public void InstantiateIn(Control container)
        {
            RadTextBox RadTextBox1 = new RadTextBox();
            RadTextBox1.Skin = "Vista";
            RadTextBox1.AutoPostBack = true;
            RadTextBox1.TextChanged += new EventHandler(RadTextBox1_TextChanged);
            container.Controls.Add(RadTextBox1);
        }

        void RadTextBox1_TextChanged(object sender, EventArgs e)
        {
            RadMenuItem item = ((RadTextBox)sender).Parent as RadMenuItem;

            string expression = "";

            RadGrid RadGrid1 = (RadGrid)item.Page.FindControl("RadGrid1");
            GridColumn column = RadGrid1.MasterTableView.GetColumnSafe(item.Value);

            if (column.DataType == typeof(string))
            {
                expression = String.Format(@"{0}.Contains(""{1}"")", item.Value, ((RadTextBox)sender).Text);
            }
            else
            {
                expression = String.Format("{0} == {1}", item.Value, ((RadTextBox)sender).Text);
            }

            RadGrid1.MasterTableView.FilterExpression = expression;
            RadGrid1.Rebind();
        }

        #endregion
    }
}
