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

/// <summary>
/// Summary description for MyCustomFilteringColumn
/// </summary>
///
namespace CustomColumnNamespace
{
	public class MyCustomFilteringColumn : GridBoundColumn
	{
		private object listDataSource = null;
		//RadGrid calls this method when it initializes the controls inside the filtering item cells
		protected override void SetupFilterControls(TableCell cell)
		{
			base.SetupFilterControls(cell);
			cell.Controls.RemoveAt(0);
			RadDropDownList list = new RadDropDownList();
			list.ID = "list" + this.DataField;
			list.AutoPostBack = true;
			list.SelectedIndexChanged += new DropDownListEventHandler(list_SelectedIndexChanged);
			list.Width = this.FilterControlWidth;
			list.DefaultMessage = this.FilterControlAltText;
			cell.Controls.AddAt(0, list);
			cell.Controls.RemoveAt(1);
			list.DataTextField = this.ColumnName;
			list.DataValueField = this.ColumnName;
			list.DataSource = this.ListDataSource;
			list.DataBind();
			list.Skin = "Vista";
		}

		void list_SelectedIndexChanged(object sender, DropDownListEventArgs e)
		{

			GridFilteringItem filterItem = (sender as RadDropDownList).NamingContainer as GridFilteringItem;
			if (this.DataType == System.Type.GetType("System.Int32") || this.DataType == System.Type.GetType("System.Int16") || this.DataType == System.Type.GetType("System.Int64"))
			{
				//filterItem.FireCommandEvent("Filter", new Pair("EqualTo", this.UniqueName));
			}
			else
				// treat everything else like a string  
				if ((sender as RadDropDownList).SelectedValue == "")
				{
					filterItem.FireCommandEvent("CancelAll", String.Empty);
				}
				else { }
			//filterItem.FireCommandEvent("Filter", new Pair("Contains", this.UniqueName));
		}

		public object ListDataSource
		{
			get
			{
				return this.listDataSource;
			}
			set
			{
				listDataSource = value;
			}
		}
		//RadGrid calls this method when the value should be set to the filtering input control(s) 
		protected override void SetCurrentFilterValueToControl(TableCell cell)
		{
			base.SetCurrentFilterValueToControl(cell);
			RadDropDownList list = (RadDropDownList)cell.Controls[0];
			if (this.CurrentFilterValue != string.Empty)
			{
				list.SelectedValue = this.CurrentFilterValue;
			} 
			
		}
		//RadGrid calls this method to extract the filtering value from the filtering input control(s) 
		protected override string GetCurrentFilterValueFromControl(TableCell cell)
		{
			RadDropDownList list = (RadDropDownList)cell.Controls[0];
			
			return list.SelectedValue;
		}
		protected override string GetFilterDataField()
		{
			return this.DataField;
		}

		public string TableName { get; set; }

		public string ColumnName { get; set; }

		public string Where { get; set; }
	}
}


