using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Telerik.Web.UI;

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

    }

    protected string CustomFilterExpression
    {
        get { return ViewState["CustomFilterExpression"] as string ?? string.Empty; }
        set { ViewState["CustomFilterExpression"] = value; }
    }
    protected void MasterTableView_DataBinding(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(CustomFilterExpression) && !RadGrid1.MasterTableView.FilterExpression.Contains(CustomFilterExpression))
        {
            ApplyCustomFilter(RadGrid1.MasterTableView, CustomFilterExpression);
        }
    }

    protected void ApplyCustomFilter(GridTableView tableView, string customFilter)
    {
        if (customFilter != string.Empty)
        {            
            if (tableView.FilterExpression == String.Empty)
            {
                tableView.FilterExpression = customFilter;
            }
            else
            {
                string[] expressions = RadGrid1.MasterTableView.FilterExpression.Split(new string[] { "([ShipCountry]" }, StringSplitOptions.RemoveEmptyEntries);

                if (expressions.Length == 2)
                {
                    RadGrid1.MasterTableView.FilterExpression = expressions[0];
                }
                else if (RadGrid1.MasterTableView.FilterExpression != expressions[0])
                {
                    RadGrid1.MasterTableView.FilterExpression = "";
                }
                else
                {
                    RadGrid1.MasterTableView.FilterExpression += " AND ";
                }
                tableView.FilterExpression = tableView.FilterExpression + " (" + customFilter + ")";
            }
        }
    }
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == Telerik.Web.UI.RadGrid.FilterCommandName)
        {
            Pair filterPair = (Pair)e.CommandArgument;
            string filterpair = filterPair.Second.ToString();

            switch (filterPair.Second.ToString())
            {
                case "ShipCountry":

                    TextBox tbPattern = (e.Item as Telerik.Web.UI.GridFilteringItem)["ShipCountry"].Controls[0] as TextBox;
                    StringBuilder newFilter = new System.Text.StringBuilder();
                        string[] values = tbPattern.Text.Split(new char[] {','} , StringSplitOptions.RemoveEmptyEntries);

                        if (values.Length > 0)
                        {
                            e.Canceled = true;

                            for (int i = 0; i < values.Length; i++)
                            {
                                if (i == 0)
                                    newFilter.Append("[ShipCountry] = '" + values[i] + "'");

                                else
                                    newFilter.Append(" OR " + "[ShipCountry] = '" + values[i] + "'");
                            }


                            CustomFilterExpression = newFilter.ToString();
                            ApplyCustomFilter(RadGrid1.MasterTableView, CustomFilterExpression);
                        }
                        else
                        {
                            CustomFilterExpression = "";
                        }
                        RadGrid1.Rebind();

                    break;

                case "OrderID":
                    TextBox tbPatternOrderid = (e.Item as Telerik.Web.UI.GridFilteringItem)["OrderID"].Controls[0] as TextBox;
                    StringBuilder newFilterOrderid = new System.Text.StringBuilder();
                    string str = RadGrid1.MasterTableView.FilterExpression;
                    break;
                default:
                    /* RadGrid1.MasterTableView.FilterExpression = "((" + RadGrid1.MasterTableView.FilterExpression + ") AND (" + newFilter.ToString() + "))";
                     RadGrid1.Rebind();*/

                    break;


            }
        }

    }
}
