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

RadGrid Column FilterTemplate as combobox with Auto Generated Columns

3 Answers 119 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Majd
Top achievements
Rank 1
Majd asked on 15 Sep 2014, 05:44 PM
I have a RadGrid where the columns are autogenerated based on the DataSet.  I need to check if one of the columns exists, I would like to make that column filtered by a combobox or a dropdown.I have a RadGrid where the columns are autogenerated based on the DataSet.  I need to check if one of the columns exists, I would like to make that column filtered by a combobox or a dropdown.I have a RadGrid where the columns are autogenerated based on the DataSet.  I need to check if one of the columns exists, I would like to make that column filtered by a combobox or a dropdown.

ASPX
<telerik:RadGrid ID="rgApplications" runat="server" AllowSorting="True" AutoGenerateColumns="False" AllowPaging="True" GridLines="None" Width="100%" CellSpacing="0">
    <MasterTableView DataKeyNames="ExecutedDocumentId,TemplateId,ParentId" ClientDataKeyNames="ExecutedDocumentId,TemplateId,ParentId" AutoGenerateColumns="true" AllowFilteringByColumn="true">
        <EditFormSettings EditFormType="Template">
            <EditColumn UniqueName="EditColumn">
            </EditColumn>
            <FormTemplate>
                <div style="padding-bottom: 5px; padding-top: 5px; padding-left: 20px;">
                    <b><%# (Container is GridEditFormInsertItem) ? GetResource("Add") : GetResource("Edit") %></b>
                    <table border="0" cellpadding="2" width="100%">
                        <tr>
                            <td nowrap>
                                <asp:Label ID="lblType" runat="server" resourcekey="Type" AssociatedControlID="ddlType" />:
                            </td>
                            <td nowrap align="left" style="padding-left: 5px">
                                <asp:DropDownList ID="ddlType" Width="340px" MaxLength="120" runat="server" />
                                <span style="color: Red; padding-left: 2px; padding-right: 3px; font-size: larger">*</span>
                                <asp:RequiredFieldValidator ID="rfvType" ControlToValidate="ddlType" InitialValue="0"
                                    ValidationGroup="UpdateApplications" runat="server" ErrorMessage="Required" ForeColor="Red" />
                            </td>
                        </tr>
                    </table>
                    <p>
                        <asp:ImageButton ID="btnSaveApplications" runat="server" ValidationGroup="UpdateApplications" resourcekey="Save" CommandName="SaveApplication" />
                          
                        <asp:ImageButton ID="btnCancelApplications" runat="server" resourcekey="Cancel" CommandName="Cancel" />
                    </p>
                </div>
            </FormTemplate>
        </EditFormSettings>
        <CommandItemSettings ShowRefreshButton="false" ShowAddNewRecordButton="true" />
        <Columns>
            <telerik:GridClientSelectColumn UniqueName="SelectApplication" HeaderTooltip="Select All" />
            <telerik:GridButtonColumn UniqueName="DeleteDocument" CommandName="DeleteDocument" ButtonType="ImageButton" ShowFilterIcon="false" />
            <telerik:GridButtonColumn UniqueName="ViewDocument" CommandName="ViewDocument" ButtonType="ImageButton" ShowFilterIcon="false" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>


Code Behind
void rgApplications_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
{
    // Make Document Status Filter A DropDown
    if (e.Column.UniqueName == "DocumentStatus")
    {
        GridBoundColumn bCol = e.Column as GridBoundColumn;
        if (bCol != null)
        {
            Data.TaxonomyFilteringTemplate template = new MyFilteringTemplate(e.Column.UniqueName, "Document Status", rgApplications);
            bCol.FilterTemplate = template;
            bCol.CurrentFilterFunction = GridKnownFunction.EqualTo;
        }
    }
}

MyFilterTemplate
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
 
namespace MyTestProgram
{
    public class MyFilteringTemplate : ITemplate
    {
        protected DropDownList combo;
        private string colname;
        private string lookupName;
        RadGrid radgrid;
 
        public TaxonomyFilteringTemplate(string cName, string lookup, RadGrid grid)
        {
            colname = cName;
            lookupName = lookup;
            radgrid = grid;
        }
 
        public void InstantiateIn(Control container)
        {
            combo = new DropDownList();
            combo.ID = "RadComboBoxControl";
            container.Controls.Add(combo);
            combo.AutoPostBack = true;
            //combo.EnableViewState = false;
            combo.SelectedIndexChanged += combo_SelectedIndexChanged;
 
            combo.Items.Add(new ListItem("Please Select"));
 
            var dt = MyController.GetLookupList(this.lookupName);
            var dvOptions = new DataView(dt);
            dvOptions.Sort = "Name";
             
 
            foreach (DataRow dr in dvOptions.ToTable().Rows)
            {
                combo.Items.Add(new ListItem(dr["Name"].ToString()));
            }
 
        }
 
        void combo_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = sender as DropDownList;
            string filterExpression = string.Empty;
            if (ddl.Text != "")
                filterExpression = "([" + colname + "] = '" + ddl.Text + "')";
            radgrid.MasterTableView.FilterExpression = filterExpression;
            radgrid.MasterTableView.Rebind();
        }
    }
}


The problem I am having is that I am getting an exception:

Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.

Any suggestion on how to solve this?

3 Answers, 1 is accepted

Sort by
0
Accepted
Eyup
Telerik team
answered on 18 Sep 2014, 11:20 AM
Hi Majd,

I've already replied to your query in ticket with ID: 859819. I suggest that we continue our conversation on the mentioned thread should you come upon any difficulties or have further questions.

Regards,
Eyup
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.

 
0
Maurizio Piaggio
Top achievements
Rank 1
answered on 11 Aug 2015, 02:23 PM

Hi Eyup,

could you please also post here the correct solution?

Would be intersting to know which are the correct steps​: in my ​scenario I should have a multiple choice filter based on the autogenerated column's values, to filter the column itself ​(DropDownTree with checkboxes? Simple dropdown with multiple values?)

0
Eyup
Telerik team
answered on 14 Aug 2015, 06:19 AM
Hi Simone,

You can find the sample here - web site number 2:
http://www.telerik.com/forums/how-to-persist-dropdownlist-selected-index-and-value-on-postback-from-radgrid-filtertemplate#GW3MyQLmVEmy8XzsmrHzeQ

I hope it will prove helpful. You can also examine the other samples.

Regards,
Eyup
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Majd
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Maurizio Piaggio
Top achievements
Rank 1
Share this question
or