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

RadGrid Filtering using Custom Dropdown column

1 Answer 82 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Abilis Solutions
Top achievements
Rank 1
Abilis Solutions asked on 14 Aug 2012, 06:23 PM
I have created a Grid with filtering using the RadGrid with the following markup

 

<telerik:RadGrid ID="RadGrid1" Width="99%" OnNeedDataSource="Grid_NeedDataSource"
                    AllowFilteringByColumn="True" AllowSorting="True" PageSize="20" ShowFooter="True"
                    AllowPaging="True" runat="server" AutoGenerateColumns="False" GridLines="None"
                    Skin="Office2007">
                    <GroupingSettings CaseSensitive="false" />
                    <MasterTableView ShowHeadersWhenNoRecords="true" AllowFilteringByColumn="True" ShowFooter="True"
                        DataKeyNames="OffenseTypeId" TableLayout="Fixed" ClientDataKeyNames="OffenseTypeId, StateCode, Description">
                        <Columns>
                            <telerik:GridClientSelectColumn HeaderStyle-Width="30px" />
                            <telerik:GridBoundColumn DataField="StateCode" HeaderText="VCC Code" AutoPostBackOnFilter="true"
                                CurrentFilterFunction="Contains" ShowFilterIcon="true" FilterControlWidth="70%">
                                <HeaderStyle Width="120px" />
                            </telerik:GridBoundColumn>
                            <custom:OffenseModifierFilteringColumn DataField="ModifierDescription" HeaderText="Offense Modifier"
                                AllowFiltering="true" FilterControlWidth="90%">
                                <HeaderStyle Width="200px" />
                                <ItemTemplate>
                                    <%# Eval("ModifierDescription")%>
                                </ItemTemplate>
                            </custom:OffenseModifierFilteringColumn>
                            <telerik:GridBoundColumn DataField="Description" HeaderText="Description" AutoPostBackOnFilter="true"
                                CurrentFilterFunction="Contains" ShowFilterIcon="true" FilterControlWidth="95%">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="OffenseStatute" HeaderText="Statute" AutoPostBackOnFilter="true"
                                CurrentFilterFunction="Contains" ShowFilterIcon="true" FilterControlWidth="70%">
                                <HeaderStyle Width="120px" />
                            </telerik:GridBoundColumn>
                        </Columns>
                    </MasterTableView>
                    <ClientSettings>
                        <Selecting AllowRowSelect="true" />
                        <ClientEvents OnRowSelected="RowSelected" />
                    </ClientSettings>
                </telerik:RadGrid>

with the custom filtering column as such:

/// <summary>
/// Custom Filtering column for the SelectOffense
/// </summary>
public class OffenseModifierFilteringColumn : GridTemplateColumn
{
    /// <summary>
    /// Setups the filter controls.
    /// </summary>
    /// <param name="cell">The cell.</param>
    protected override void SetupFilterControls(TableCell cell)
    {
        var rcBox = new RadComboBox
                        {
                            ID = "DropDownList1",
                            AutoPostBack = true,
                            DataTextField = DataField,
                            DataValueField = DataField
                        };
        rcBox.SelectedIndexChanged += rcBox_SelectedIndexChanged;
        var table = GetDataTable();
        var row = table.NewRow();
        row[DataField] = "";
        table.Rows.InsertAt(row, 0);
        rcBox.DataSource = table;
        cell.Controls.Add(rcBox);
    }
    /// <summary>
    /// Sets the current filter value to control.
    /// </summary>
    /// <param name="cell">The cell.</param>
    protected override void SetCurrentFilterValueToControl(TableCell cell)
    {
        if (!(CurrentFilterValue == ""))
        {
            ((RadComboBox)cell.Controls[0]).Items.FindItemByText(CurrentFilterValue).Selected = true;
        }
    }
    /// <summary>
    /// Gets the current filter value from control.
    /// </summary>
    /// <param name="cell">The cell.</param>
    /// <returns></returns>
    protected override string GetCurrentFilterValueFromControl(TableCell cell)
    {
        var currentValue = ((RadComboBox)cell.Controls[0]).SelectedItem.Value;
        CurrentFilterFunction = (currentValue != "") ? GridKnownFunction.EqualTo : GridKnownFunction.NoFilter;
        return currentValue;
    }
    /// <summary>
    /// Handles the SelectedIndexChanged event of the rcBox control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs"/> instance containing the event data.</param>
    protected void rcBox_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        ((GridFilteringItem)(((RadComboBox)sender).Parent.Parent)).FireCommandEvent("Filter", new Pair());
    }
    /// <summary>
    /// Gets the filter data field.
    /// </summary>
    /// <returns></returns>
    protected override string GetFilterDataField()
    {
        return DataField;
    }
    #region Private Members
    /// <summary>
    /// Gets the data table.
    /// </summary>
    /// <returns></returns>
    public DataTable GetDataTable()
    {
        var offenseModifiers = BusinessProcessManagerFactory<IOffenseTypeManager>.Instance.Get().GetOffenseModifiers();
        // convert to datatable
        var myDataTable = SqlTableTypeHelper.CreateTableType(offenseModifiers.Select(x => x.ModifierDescription).ToList(), typeof(String), "ModifierDescription");
        return myDataTable;
    }
    #endregion
}

As per your example in Grid -> Filtering Template Columns. I am using Telerik version  2009.3.1208.35.

The page render's and the grid display full of data. I filter on every column except the custom column and the filtering works great. When I select a value from the drop down, the grid empties and no records are displayed.

Any idea's?

Thanks,
Luke

1 Answer, 1 is accepted

Sort by
0
Milena
Telerik team
answered on 17 Aug 2012, 11:28 AM
Hello Luke,

We are not able to run the provided code, because there are some missing resources from your project. However, we reviewed your code and could not find any obvious reason for the observed behavior. Generally, we recommend the use of the latest RadControls release since on every new release, new changes are being made for the improvement and usability of the controls. In case the problem still exists with our latest version, it would be helpful to isolate the problem in a small working project and we will investigate the issue.


Greetings,
Milena
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Abilis Solutions
Top achievements
Rank 1
Answers by
Milena
Telerik team
Share this question
or