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

Set FilterTemplate value on page load

2 Answers 358 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marc
Top achievements
Rank 1
Marc asked on 23 May 2014, 02:20 PM
Hi

On page load I want to set one of the custom filters if querystring has a value.  I am getting an exception (no items in collection) in my CombineFilters method on line

GridFilteringItem filterItem = grdParts.MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem;

I believe this is mostly about the order I am doing things in (page load running before filter pre render event) 
but I am not sure how to modify the code to get this working. 

Code attached below

Check QueryString, if company passed in have the filter preselected
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindGrid();
    }
 
    //if (querystring has value)
    //{
        ViewState["CompanyFilterVal"] = "123";
        CombineFilters();
    //}
}

Setting the filter dropdown values on grid item created event
//Item created - binding data to custom company filter 
protected void grdParts_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridFilteringItem)
    {
        try
        {
            using (MyEntities db = new MyEntities())
            {
                //Set options for the filter controls
                GridFilteringItem filterItem = (GridFilteringItem)e.Item;
 
                RadComboBox CompanyCbo = (RadComboBox)filterItem["Company"].FindControl("cboFilterCompany");
                BindCompanyFilterData(db, CompanyCbo);
 
                //...other filters
        }
        catch (Exception ex)
        {
            ...
        }
    }
}

setting selected value on filter pre render event. 
protected void cboFilterCompany_PreRender(object sender, EventArgs e)
{
    RadComboBox combo = sender as RadComboBox;
    if (ViewState["CompanyFilterVal"] != null)
        combo.SelectedValue = ViewState["CompanyFilterVal"].ToString();
    else
        combo.ClearSelection();
}

method inspects all filters for value and sets grid filter based on filters
protected void CombineFilters()
{
    string filterToApply = "";
    List<string> filtexprn = new List<string>();
 
    ClearFilterFromViewState();
 
    GridFilteringItem filterItem = grdParts.MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem;
 
    //COMPANY
    RadComboBox cboCompany = (RadComboBox)filterItem.FindControl("cboFilterCompany");
    if (cboCompany.SelectedIndex != -1)
    {
        if(cboCompany.SelectedValue != "All")
        {
            filtexprn.Add("([Company] ='" + cboCompany.SelectedValue + "')");
            ViewState["CompanyFilterVal"] = cboCompany.SelectedValue; //to persist value on postback
        }
    }
 
    //...
     
    //SITE
    RadComboBox cboSite = (RadComboBox)filterItem.FindControl("cboFilterSite");
    if (cboSite.SelectedIndex != -1)
    {
        if (cboSite.SelectedValue != "All")
        {
            filtexprn.Add("([Site] ='" + cboSite.SelectedValue + "')");
            ViewState["SiteFilterVal"] = cboSite.SelectedValue; //to persist value on postback
        }
    }
 
    int count = filtexprn.Count - 1;
    foreach (string s in filtexprn)
    {
        filterToApply += s;
        if (count-- != 0)
            filterToApply += " AND ";
    }
 
    grdParts.MasterTableView.FilterExpression = filterToApply;
    grdParts.MasterTableView.Rebind();
}

grid aspx code
<telerik:RadGrid ID="grdParts" runat="server"
    OnItemCreated="grdParts_ItemCreated"
    OnItemDataBound="grdParts_ItemDataBound"
    OnItemCommand="grdParts_ItemCommand"
    OnNeedDataSource="grdParts_NeedDataSource"
    AllowFilteringByColumn="true" AllowSorting="true"
    AllowPaging="True" PageSize="15" EnableLinqExpressions="false"
    AllowMultiRowSelection="true">
    <PagerStyle AlwaysVisible="true"></PagerStyle>
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="UniquePartID" ClientDataKeyNames="UniquePartID"
        Width="100%" CommandItemDisplay="None" PageSize="15">
        <Columns>
            <telerik:GridBoundColumn DataField="UniquePartID" HeaderText="UniquePartID" ReadOnly="True"
                SortExpression="UniquePartID" UniqueName="UniquePartID" Display="false">
            </telerik:GridBoundColumn>
 
            <telerik:GridBoundColumn DataField="Company" HeaderText="CONO" SortExpression="Company" UniqueName="Company" FilterControlAltText="Filter Company column"
                ItemStyle-Width="4%" HeaderStyle-Width="4%" AllowFiltering="true" AllowSorting="true">
                <FilterTemplate>
                    <telerik:RadComboBox runat="server" ID="cboFilterCompany" AutoPostBack="false"
                        OnPreRender="cboFilterCompany_PreRender"
                        DataValueField="CompanyID" DataTextField="CompanyDescription"
                        Width="100%">
                    </telerik:RadComboBox>
                </FilterTemplate>
            </telerik:GridBoundColumn>
 
            <telerik:GridBoundColumn DataField="Site" HeaderText="Site" SortExpression="Site" UniqueName="Site" FilterControlAltText="Filter Site column"
                ItemStyle-Width="6%" HeaderStyle-Width="6%" AllowFiltering="true" AllowSorting="true">
                <FilterTemplate>
                    <telerik:RadComboBox runat="server" ID="cboFilterSite" AutoPostBack="false"
                        OnPreRender="cboFilterSite_PreRender"
                        DataValueField="SiteDesc" DataTextField="SiteDesc"
                        Width="100%">
                    </telerik:RadComboBox>
                </FilterTemplate>
            </telerik:GridBoundColumn>
 
            ...
 
        </Columns>
    </MasterTableView>
    <ClientSettings>
        <Selecting AllowRowSelect="true"></Selecting>
        <ClientEvents OnRowDblClick="RowDblClick"></ClientEvents>
    </ClientSettings>
    <GroupingSettings CaseSensitive="false" />
</telerik:RadGrid>

2 Answers, 1 is accepted

Sort by
0
Marc
Top achievements
Rank 1
answered on 23 May 2014, 02:24 PM
Example of grid filter attached to illustrate my problem
0
Kostadin
Telerik team
answered on 28 May 2014, 07:06 AM
Hi Marc,

I would recommend you to bind the grid by using an advanced data-binding through NeedDataSource event handler as described in the following help article. Also you could examine the following help topic which describes how to apply a filter on initial load.

I hope this helps.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Marc
Top achievements
Rank 1
Answers by
Marc
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or