I'm trying to implement a RadGrid with a dropdownlist filter column much like the demos provided on this site. The only difference is when I databind the grid I get only a filtered page of data from the database. So I don't really need the dropdownlist to do any actual filtering of the grid, just trigger the rebind of the grid so it checks the filter values and gets an appropriate page of data from the database.
Here's my custom filter column:
Here's my aspx code-behind:
And here's my grid declaration:
A couple things are happening. CurrentFilterValue is never set. I'm assuming because I call e.Canceled = true. The dropdownlists also are not saving the user selection on postbacks (also because I cancel the event?)
Any ideas?
Chris
Here's my custom filter column:
public class FilteringByDropDownBoundColumn : GridBoundColumn { private object listDataSource = null; private Unit filterControlWidth = Unit.Percentage(90); // default private string filterTextField = "Text"; private string filterValueField = "Value"; public string FilterTextField { get { return filterTextField; } set { filterTextField = value; } } public string FilterValueField { get { return filterValueField; } set { filterValueField = value; } } protected override void SetupFilterControls(TableCell cell) { base.SetupFilterControls(cell); Control oldFilter = cell.Controls[0]; cell.Controls.RemoveAt(0); DropDownList list = new DropDownList(); list.ID = "list" + DataField; list.AutoPostBack = true; list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged); list.DataTextField = FilterTextField; list.DataValueField = FilterValueField; list.DataSource = ListDataSource; list.Width = FilterControlWidth; list.Height = Unit.Pixel(20); list.EnableViewState = true; list.Attributes.Add("style", "font-size:.9em"); cell.Controls.AddAt(0, list); cell.Controls.RemoveAt(1); } void list_SelectedIndexChanged(object sender, EventArgs e) { GridFilteringItem filterItem = (sender as DropDownList).NamingContainer as GridFilteringItem; filterItem.FireCommandEvent("Filter", new Pair()); } public new Unit FilterControlWidth { get { return (Unit)filterControlWidth; } set { filterControlWidth = value; } } public object ListDataSource { get { return listDataSource; } set { listDataSource = value; } } protected override void SetCurrentFilterValueToControl(TableCell cell) { base.SetCurrentFilterValueToControl(cell); DropDownList list = (DropDownList)cell.Controls[0]; if (CurrentFilterValue != string.Empty) { list.SelectedValue = CurrentFilterValue; } } protected override string GetCurrentFilterValueFromControl(TableCell cell) { DropDownList list = (DropDownList)cell.Controls[0]; return list.SelectedValue; } protected override string GetFilterDataField() { return FilterValueField; } Here's my aspx code-behind:
protected override void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { SetupTabs(); SetupFilters(NeedsActionAccountsGrid); } private void DataBindGrid(string view, RadGrid grid) { if (grid != null && !string.IsNullOrEmpty(view)) { QueueViewType viewType = (QueueViewType)(Enum.Parse(typeof(QueueViewType), view)); Rep rep = LoggedInUser; DateTime? LastModifiedBeginDate = null; DateTime? LastModifiedEndDate = null; DateCriteria.SetDate(ref LastModifiedBeginDate, ref LastModifiedEndDate, GetLastModifiedDateCriteria(grid)); List<AccountQueueRecord> datasource = AccountQueueLogic.GetAccountsQueueItems(rep, viewType, LastModifiedBeginDate, LastModifiedEndDate, GetRegistrationTypeID(grid), grid.PageSize, grid.CurrentPageIndex); grid.DataSource = datasource; grid.DataBind(); } } private string GetRegistrationTypeID(RadGrid grid) { string RegistrationTypeID = null; FilteringByDropDownBoundColumn col = grid.Columns.FindByDataFieldSafe("Registration") as FilteringByDropDownBoundColumn; if (col != null && col.CurrentFilterValue != "All") RegistrationTypeID = col.CurrentFilterValue; return RegistrationTypeID; } private void SetupFilters(RadGrid grid) { if (grid == null) return; LookupTypeService lookupService = new LookupTypeService(); FilteringByDropDownBoundColumn col = grid.Columns.FindByDataFieldSafe("Registration") as FilteringByDropDownBoundColumn; if (col != null) { List<RegistrationType> list = new List<RegistrationType>(); list.Add(new RegistrationType() { Display = "All", Code = "All" }); list.AddRange(lookupService.GetAllEnabled(LookupTypeEnum.RegistrationType).Cast<RegistrationType>().ToList()); col.ListDataSource = list; col.FilterTextField = "Display"; col.FilterValueField = "Code"; } } protected void GridAccountQueue_ItemCommand(object source, GridCommandEventArgs e) { if (e.CommandName == RadGrid.FilterCommandName) { e.Canceled = true; RadGrid grid = (RadGrid)source; grid.MasterTableView.FilterExpression = ""; // clear this since we're providing a filtered datasource string viewID = ""; string gridID = grid.ID; switch (gridID) { case "NeedsActionAccountsGrid": case "NeedsActionProposalsGrid": viewID = NeedsActionTabs.SelectedTab.SelectedTab.Value; // go to second level of tabs break; } DataBindGrid(viewID, grid); } } And here's my grid declaration:
<telerik:RadGrid ID="NeedsActionAccountsGrid" runat="server" OnItemCommand = "GridAccountQueue_ItemCommand"
OnSortCommand="GridAccountQueue_SortCommand"OnPageSizeChanged="GridAccountQueue_PageSizeChanged" OnPageIndexChanged="GridAccountQueue_PageIndexChanged" AutoGenerateColumns="false" PageSize="10" ShowStatusBar="true" AllowSorting="true" AllowPaging="true" AllowCustomPaging="true" Skin="Simple" AllowFilteringByColumn="true"> <PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="true"/> <StatusBarSettings LoadingText="Loading..." /> <MasterTableView HeaderStyle-BackColor="Gray" HeaderStyle-ForeColor="White" EnableColumnsViewState="true" TableLayout="Auto"> <SortExpressions> <telerik:GridSortExpression FieldName="LastModified" SortOrder="Descending" /> </SortExpressions> <Columns> <telerik:GridHyperLinkColumn AllowFiltering="false" HeaderText="AC ID" DataTextField="AccountID" DataNavigateUrlFields="AccountID" DataNavigateUrlFormatString="/Administratin/AccountSummary.aspx?id={0}" HeaderStyle-Width="5%" ItemStyle-Width="5%" ItemStyle-CssClass="AccountLink" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"></telerik:GridHyperLinkColumn> <custom:FilteringByDropDownBoundColumn HeaderText="Rep code" DataField="RepCode" HeaderStyle-Width="6%" ItemStyle-Width="6%" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"></custom:FilteringByDropDownBoundColumn> <custom:FilteringByDropDownBoundColumn HeaderText="Registration" HeaderAbbr="Reg." DataField="Registration" HeaderStyle-Width="10%" ItemStyle-Width="10%"></custom:FilteringByDropDownBoundColumn> <custom:FilteringByDropDownBoundColumn HeaderText="Platform" DataField="Platform" HeaderStyle-Width="12%" ItemStyle-Width="12%"></custom:FilteringByDropDownBoundColumn> <custom:FilteringByDropDownBoundColumn HeaderText="Product Type" HeaderAbbr="Prod. Type" DataField="ProductType" HeaderStyle-Width="12%" ItemStyle-Width="12%"></custom:FilteringByDropDownBoundColumn> <telerik:GridBoundColumn HeaderText="Account Holders" HeaderAbbr="Acct. Holders" DataField="AccountHolders" FilterControlWidth="90%" AutoPostBackOnFilter="false"></telerik:GridBoundColumn> <custom:FilteringByDropDownBoundColumn HeaderText="Last Modified" HeaderAbbr="Last Mod." DataField="LastModified" DataFormatString="{0:M/dd/yyyy h:mm tt}" HeaderStyle-Width="13%" ItemStyle-Width="13%"></custom:FilteringByDropDownBoundColumn> <telerik:GridBoundColumn AllowFiltering="false" HeaderText="Status" DataField="Status" HeaderStyle-Width="15%" ItemStyle-Width="15%" ></telerik:GridBoundColumn> <telerik:GridButtonColumn ButtonType="ImageButton" HeaderText="Notes" HeaderStyle-Width="4%" ItemStyle-Width="4%" ImageUrl="../Images/grid/icon_attachment.gif" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"></telerik:GridButtonColumn> <telerik:GridButtonColumn ButtonType="ImageButton" HeaderText="Docs" HeaderStyle-Width="4%" ItemStyle-Width="4%" ImageUrl="../Images/grid/icon_attachment.gif" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"></telerik:GridButtonColumn> </Columns> </MasterTableView> <ClientSettings> <Resizing AllowColumnResize="true" /> </ClientSettings> </telerik:RadGrid> A couple things are happening. CurrentFilterValue is never set. I'm assuming because I call e.Canceled = true. The dropdownlists also are not saving the user selection on postbacks (also because I cancel the event?)
Any ideas?
Chris