This is a migrated thread and some comments may be shown as answers.

Dropdownlist filter with filtered datasource

3 Answers 198 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 28 Mar 2011, 11:04 PM
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:
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

3 Answers, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 31 Mar 2011, 01:20 PM
Hello Chris,

In order to implement the desired functionality, I recommend that you use the Filter Templates feature of the RadGrid. It provides a straight-forward approach to enable dropdown filters for GridBoundColumns (or those columns which inherit from GridBoundColumns like GridDateTimeColumn, GridNumericColumn, etc.) and GridTemplateColumns declaratively.

I hope this helps.

All the best,
Mira
the Telerik team
0
Chris
Top achievements
Rank 1
answered on 31 Mar 2011, 07:22 PM
Thanks Mira. I had actually switched to the FilterTemplate approach which solved my problems except I need to know how to get my filter control in the column outside of the ItemDataBound event handler. I think I saw the code in one of the forums but I can no longer find it. It was something like:

GridFilteringItem item = radgrid.MasterTableView.Controls[0];


But I know that's not right.

Thanks,
Chris

0
Mira
Telerik team
answered on 06 Apr 2011, 08:10 AM
Hello Chris,

In order to implement the desired functionality, I recommend that you use the GetItems method of the master table view to get the filtering item.
Please examine the Using the GetItems(itemType), GetColumn(columnName) and GetColumnSafe(columnName) methods help topic for additional information.

I hope this helps.

All the best,
Mira
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Mira
Telerik team
Chris
Top achievements
Rank 1
Share this question
or