I am trying to implement a custom filter using the DropDownList control as documented in
http://www.telerik.com/help/aspnet-ajax/grid-filtering-with-dropdownlist.html
However, I am generating the RadGrid columns at runtime in the Page_Load event.
I receive the error "Exception of type 'System.Web.HttpUnhandledException' was thrown. " when I try to filter the data. The grid and data load correctly. I receive the error only when I try to filter the data. The exception is trapped in the GLobal.asax. I cannot determine what is throwing the exception.
The RadGrid filtering works fine if I do not use the MyCustomFilteringColumn and simply use the GridBoundColumn for all columns that are created.
The project uses the Adventure Works 2008R2 database. I am displaying all records from the DimCustomer table. The "FirstName" column implements the custom control "MyCustomFilteringColumn : GridBoundColumn". All other columns are "GridBoundColumn".
I'm not sure what I'm missing. Any help would be greatly appreciated.
Thanks,
Mike
http://www.telerik.com/help/aspnet-ajax/grid-filtering-with-dropdownlist.html
However, I am generating the RadGrid columns at runtime in the Page_Load event.
I receive the error "Exception of type 'System.Web.HttpUnhandledException' was thrown. " when I try to filter the data. The grid and data load correctly. I receive the error only when I try to filter the data. The exception is trapped in the GLobal.asax. I cannot determine what is throwing the exception.
The RadGrid filtering works fine if I do not use the MyCustomFilteringColumn and simply use the GridBoundColumn for all columns that are created.
The project uses the Adventure Works 2008R2 database. I am displaying all records from the DimCustomer table. The "FirstName" column implements the custom control "MyCustomFilteringColumn : GridBoundColumn". All other columns are "GridBoundColumn".
I'm not sure what I'm missing. Any help would be greatly appreciated.
Thanks,
Mike
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewMemberDetails.aspx.cs" Inherits="BillingPortal.ViewMemberDetails" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <link href="Styles/VisualIdentity.css" rel="stylesheet" type="text/css" /></head><script type="text/javascript"> function PopupClose(button) { window.close(); }</script><body> <form id="form1" runat="server"> <telerik:RadScriptManager ID="RadScriptManager1" runat="server"> <Scripts> <%--Needed for JavaScript IntelliSense in VS2010--%> <%--For VS2008 replace RadScriptManager with ScriptManager--%> <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" /> <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" /> <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" /> </Scripts> </telerik:RadScriptManager> <div> <center> <h1><font color="#00458a"> Test RadGrid Custom Filter </font></h1> <telerik:RadGrid ID="RadGrid1" OnNeedDataSource="RadGrid2_NeedDataSource" AllowPaging="True" runat="server" GridLines="None" Width="1199px" CellSpacing="0" AllowFilteringByColumn="True" AutoGenerateColumns="False" Skin="Windows7" > <MasterTableView Width="99%" CommandItemDisplay="Top" > <CommandItemSettings ShowAddNewRecordButton="false" ShowExportToExcelButton="false" ShowExportToWordButton="false" ShowExportToCsvButton="false" /> <Columns> </Columns> </MasterTableView> </telerik:RadGrid> <h3 /> <h3><asp:Label ID="lblError" runat="server" ForeColor="Red" Visible="False"></asp:Label> </h3> <h3 /> <telerik:RadButton ID="btnClose" runat="server" OnClientClicked="PopupClose" Text="Close"></telerik:RadButton> </center> </div> </form></body></html> public partial class ViewMemberDetails : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { lblError.Visible = false; if (!IsPostBack) { DataTable dt; dt = GetData(); // build RadGrid columns RadGrid1.MasterTableView.Columns.Clear(); foreach (DataColumn dataColumn in dt.Columns) { if (dataColumn.ColumnName == "FirstName") { MyCustomFilteringColumn gridColumn = new MyCustomFilteringColumn(); RadGrid1.MasterTableView.Columns.Add(gridColumn); gridColumn.DataField = dataColumn.ColumnName; gridColumn.HeaderText = dataColumn.ColumnName; gridColumn.UniqueName = dataColumn.ColumnName; } else { GridBoundColumn boundColumn = new GridBoundColumn(); RadGrid1.MasterTableView.Columns.Add(boundColumn); boundColumn.DataField = dataColumn.ColumnName; boundColumn.HeaderText = dataColumn.ColumnName; boundColumn.UniqueName = dataColumn.ColumnName; } } } } protected void RadGrid2_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) { RadGrid1.DataSource = GetData(); } public DataTable GetData() { string sConnectionString; sConnectionString = "Server=.;Database=AdventureWorksDW2008R2;Trusted_Connection=Yes;"; SqlConnection conn = new SqlConnection(sConnectionString); SqlCommand cmd = new SqlCommand(); SqlDataAdapter adapter = new SqlDataAdapter(); DataSet dsSummary = new DataSet(); DataTable dtSummary; try { conn.Open(); cmd = new SqlCommand("SELECT * FROM [AdventureWorksDW2008R2].[dbo].[DimCustomer]", conn); cmd.CommandType = CommandType.Text; adapter.SelectCommand = cmd; adapter.Fill(dsSummary); } catch (Exception ex) { this.lblError.Visible = true; this.lblError.Text = ex.Message; } finally { conn.Close(); } dtSummary = dsSummary.Tables[0]; return dtSummary; } 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 = this.DataField; list.DataValueField = this.DataField; //list.DataSource = this.ListDataSource; list.DataSource = this.ListDataSource2; //string[] s1 = new string[3] { "John", "Paul", "Mary" }; //ListDataSource = s1; } 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; } } public DataTable ListDataSource2 { get { // Build list values DataTable dtDropDownValues = new DataTable(); dtDropDownValues.Columns.Add(this.DataField); DataRow _dr = dtDropDownValues.NewRow(); _dr[this.DataField] = "Mike"; dtDropDownValues.Rows.Add(_dr); DataRow _dr2 = dtDropDownValues.NewRow(); _dr2[this.DataField] = "Rob"; dtDropDownValues.Rows.Add(_dr2); DataRow _dr3 = dtDropDownValues.NewRow(); _dr3[this.DataField] = "Jim"; dtDropDownValues.Rows.Add(_dr3); return dtDropDownValues; } } //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; } }