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
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
Setting the filter dropdown values on grid item created event
setting selected value on filter pre render event.
method inspects all filters for value and sets grid filter based on filters
grid aspx code
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>