Telerik Grid Filtering with MS DropDownList Instead of Textbox

原创 2012年03月29日 00:28:27

Reference:http://www.telerik.com/help/aspnet-ajax/grid-filtering-with-dropdownlist.html


1. Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="TelerikGridCustomFilteringColumn._Default" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register Namespace="TelerikGridCustomFilteringColumn" TagPrefix="custom" Assembly="TelerikGridCustomFilteringColumn"  %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">
            www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
                            <AjaxSettings>
                                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                                    <UpdatedControls>
                                        <telerik:AjaxUpdatedControl ControlID="RadGrid1"/>
                                    </UpdatedControls>
                                </telerik:AjaxSetting>
                            </AjaxSettings>
                        </telerik:RadAjaxManager>
    <telerik:RadGrid id="RadGrid1" runat="server" allowfilteringbycolumn="True" allowpaging="True"
        pagesize="5" onneeddatasource="RadGrid1_NeedDataSource">
         <MasterTableView
            AutoGenerateColumns="false"
            CommandItemDisplay="Top">
           <Columns>
             <custom:MyCustomFilteringColumn
                HeaderText="Employee ID"
                DataField="EmployeeID"
                UniqueName="EmployeeID" AutoPostBackOnFilter="true"/>
             <telerik:GridBoundColumn
                AllowFiltering="False"
                HeaderText="Customer ID"
                DataField="CustomerID"
                UniqueName="CustomerID" />
             <telerik:GridBoundColumn
                AllowFiltering="False"
                HeaderText="Order ID"
                DataField="OrderID"
                UniqueName="OrderID" />
           </Columns>
         </MasterTableView>
        </telerik:RadGrid>
</asp:Content>

2. Default.aspx.cs

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

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

        private List<KeyValuePair<string, string>> GetFilterTable(string Field)
        {
            List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();

            KeyValuePair<string, string> a = new KeyValuePair<string, string>("Employee1", "C1");
            KeyValuePair<string, string> b = new KeyValuePair<string, string>("Employee2", "C2");
            KeyValuePair<string, string> c = new KeyValuePair<string, string>("Employee3", "C3");

            list.Add(a);
            list.Add(b);
            list.Add(c);

            return list;
        }

        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            foreach (GridBoundColumn column in RadGrid1.MasterTableView.Columns)
            {
                if (column is TelerikGridCustomFilteringColumn.MyCustomFilteringColumn)
                {
                    (column as TelerikGridCustomFilteringColumn.MyCustomFilteringColumn).ListDataSource = GetFilterTable(column.DataField);
                }
            }

            RadGrid1.DataSource = new List<Orders>()
            {
                new Orders("C1","aa","bb"),
                new Orders("C1","aa","bb"),
                new Orders("C2","aa","bb"),
                new Orders("C2","aa","bb"),
                new Orders("C2","aa","bb"),
                new Orders("C2","aa","bb")
            };
        }
    }

    public class Orders
    {
        public string EmployeeID {get;set;}
        public string CustomerID {get;set;}
        public string OrderID {get;set;}

        public Orders(string e, string c, string o)
        {
            EmployeeID = e;
            CustomerID = c;
            OrderID = o;
        }
    }
}

3. MyCustomFilteringColumn.cs

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

namespace TelerikGridCustomFilteringColumn
{
    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);
            DropDownList list = new DropDownList();
            list.ID = "list" + this.DataField;
            list.AutoPostBack = true;
            list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
            cell.Controls.AddAt(0, list);
            cell.Controls.RemoveAt(1);
            list.DataTextField = "Key";
            list.DataValueField = "Value";
            list.DataSource = this.ListDataSource;
        }
        void list_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridFilteringItem filterItem = (sender as DropDownList).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   
                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);
            DropDownList list = (DropDownList)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)
        {
            DropDownList list = (DropDownList)cell.Controls[0];
            return list.SelectedValue;
        }
        protected override string GetFilterDataField()
        {
            return this.DataField;
        }
    }
}

Note: MyCustomFilteringColumn class "this.DataType"  will refer to data source data column field, if data field is int, then compare will use "EqualTo", but if data field is string, then compare will use "Contains"

void list_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridFilteringItem filterItem = (sender as DropDownList).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   
                filterItem.FireCommandEvent("Filter", new Pair("Contains", this.UniqueName));
        }


RadGrid1.DataSource = new List<Orders>()
            {
                new Orders("C1","aa","bb"),
                new Orders("C1","aa","bb"),
                new Orders("C2","aa","bb"),
                new Orders("C2","aa","bb"),
                new Orders("C2","aa","bb"),
                new Orders("C2","aa","bb")
            };


相关文章推荐

Telerik Grid Server Side Paging, Sorting and Filtering

Reference: http://ryantomlinson.com/post/True-data-paging-the-Telerik-RadGrid.aspx http://demos....

Use thrust reduce_by_key with raw pointers instead of device pointers

Thrust is a powerful library to improve the performance the program has relatively abundant of memor...

Keep the resource of the grid cluster consistency with OS status.

When I checked the resource after the OS restart ,I found some of resource was not restarted. The I...

在Telerik for silverlight控件radtreeview、textBox、RadGridView中实现拖拉控件的操作

效果图: 1.首先构造实体类Team view plain public class Team       {           publi...

reality instead of quality?

  • 2016年02月11日 18:11
  • 3.62MB
  • 下载

Telerik Kindo Grid 隐藏 Y 轴 滚动条(scroll )设置自适应高度

Telerik Kindo Grid 隐藏 Y 轴 滚动条(scroll )设置自适应高度
  • z1y1l1
  • z1y1l1
  • 2015年11月03日 11:12
  • 1894

触发器---FOR INSERT与INSTEAD OF

  • 2011年02月28日 21:06
  • 27KB
  • 下载

GridView+DropDownList+TextBox实现查询功能

今天做了好久终于实现了!记录一下~
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Telerik Grid Filtering with MS DropDownList Instead of Textbox
举报原因:
原因补充:

(最多只允许输入30个字)