using System;
using System.Data;
using Telerik.Web.UI;
using System.Linq;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
using CustomFilteringColumnNamespace;


public partial class Default : System.Web.UI.Page
{
	DataTable dt;
	private void Page_Load(object sender, System.EventArgs e)
	{
		if (!IsPostBack)
		{
			dt = GetDataTable("SELECT Country, City, PostalCode FROM Customers");
			this.RadGrid1.MasterTableView.Columns.Clear();
			foreach (DataColumn dataColumn in dt.Columns)
			{
				MyCustomFilteringColumnCS gridColumn = new MyCustomFilteringColumnCS();
				this.RadGrid1.MasterTableView.Columns.Add(gridColumn);
				gridColumn.DataField = dataColumn.ColumnName;
				gridColumn.HeaderText = dataColumn.ColumnName;
				gridColumn.HeaderStyle.Width = Unit.Pixel(220);
			}
           	GridBoundColumn boundColumn = new GridBoundColumn();
			boundColumn.DataField="Country";
			boundColumn.HeaderText = "CountryBoundCol";
			boundColumn.HeaderStyle.Width = Unit.Pixel(320);
			RadGrid1.MasterTableView.Columns.Add(boundColumn);

			GridDateTimeColumn dateTimeColumn = new GridDateTimeColumn();
			dateTimeColumn.HeaderText = "DateTimeCol";
			RadGrid1.MasterTableView.Columns.Add(dateTimeColumn);
			boundColumn.HeaderStyle.Width = Unit.Pixel(420);
		}
	}
	protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
	{
		dt = GetDataTable("SELECT Country, City, PostalCode FROM Customers");
		(sender as RadGrid).DataSource = dt;
	}
	protected void RadGrid1_ColumnCreating(object sender, GridColumnCreatingEventArgs e)
	{
		if ((e.ColumnType == typeof(MyCustomFilteringColumnCS).Name))
		{
			e.Column = new MyCustomFilteringColumnCS();
		}
	}
	protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
	{
		if ((e.CommandName == "Filter"))
		{
			foreach (GridColumn column in e.Item.OwnerTableView.Columns)
			{
				column.CurrentFilterValue = string.Empty;
				column.CurrentFilterFunction = GridKnownFunction.NoFilter;
			}
		}
	}
	protected void clrFilters_Click(object sender, EventArgs e)
	{
		foreach (GridColumn column in RadGrid1.MasterTableView.Columns)
		{
			column.CurrentFilterFunction = GridKnownFunction.NoFilter;
			column.CurrentFilterValue = string.Empty;
		}
		RadGrid1.MasterTableView.FilterExpression = string.Empty;
		RadGrid1.MasterTableView.Rebind();
	}
	public static DataTable GetDataTable(string query)
	{
		string ConnString = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
		SqlConnection conn = new SqlConnection(ConnString);
		SqlDataAdapter adapter = new SqlDataAdapter();
		adapter.SelectCommand = new SqlCommand(query, conn);
		DataTable myDataTable = new DataTable();
		conn.Open();
		try
		{
			adapter.Fill(myDataTable);
		}
		finally
		{
			conn.Close();
		}
		return myDataTable;
	}
}
