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

public partial class TestFilteringGrid : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);
        RadGrid1.ItemDataBound += new GridItemEventHandler(RadGrid1_ItemDataBound);
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.FilterCommandName)
        {
            GridFilteringItem item = e.Item as GridFilteringItem;
            TableCell cell = item[((Pair)e.CommandArgument).Second.ToString()];
            TextBox textBox = cell.Controls[0] as TextBox;
            string selectedValie = textBox.Text;

            string choosedFilterOption = ((Pair)e.CommandArgument).First.ToString();
        }
    }

    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = GetDataTable("SELECT * FROM Products");
    }

    public DataTable GetDataTable(string query)
    {
        String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].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;
    }

    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {

        }

        if (e.Item is GridGroupFooterItem)
        {

        }

        if (e.Item is GridGroupHeaderItem)
        {
            GridGroupHeaderItem item = (GridGroupHeaderItem)e.Item;
            item.Controls.Add(new TableCell());
            item.Controls.Add(new TableCell());
        }
    }

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            var item = e.Item as GridDataItem;
            int productID = Convert.ToInt32(item.GetDataKeyValue("ProductID"));

            Hashtable values = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(values, item);
        }

        if (e.Item is GridGroupHeaderItem)
        {
            GridGroupHeaderItem item = (GridGroupHeaderItem)e.Item;
            DataRowView groupDataRow = (DataRowView)e.Item.DataItem;
            string[] groupHeaderExpressions = item.DataCell.Text.Split(';');
            item.DataCell.Text = groupHeaderExpressions[0].Split(':')[1];
            ((TableCell)item.Controls[2]).Text = groupHeaderExpressions[0].Split(':')[1];
        }
        if (e.Item is GridNestedViewItem)
        {
        }
    }
}