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

Filtering on GridTemplateColumn, binding to a list

4 Answers 172 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Coy
Top achievements
Rank 1
Coy asked on 08 Feb 2011, 09:58 PM
Hello,

I've got a radgrid with a GridTemplateColumn that has a Label that I set to display my data within the column.  I do this because the object that I'm binding to contains a list of objects.  Using a GridTemplateColumn allows me to display these 1 to many values the way I want to display them.

Now, filtering does not seem to work correctly with this setup.

I've searched the forum and found that you can set the DataField property on the GridTemplateColumn and the filtering will use that field.

Unfortunately, I'm not able to set the DataField property because I want to search within a list of objects.

If I place the name of the List, in this case ICodes, in the DataField property, and then perform a filter, I receive an error.

What is the best way to handle my scenario?

I want to allow the user to type a string within the filter box and use that string to filter within a list.  Is this possible?  Is there an event I can handle to do this work on my own?

Thanks!

Coy

4 Answers, 1 is accepted

Sort by
0
Coy
Top achievements
Rank 1
answered on 08 Feb 2011, 11:05 PM
I'm looking into using a FilterTemplate with a textbox and a image button to do my own custom filtering.

It's working so far.
0
Accepted
Veli
Telerik team
answered on 14 Feb 2011, 09:27 AM
Hello Coy,

This scenario is not automatically supported in RadGrid. You will have to implement custom filtering. You can use the FilterTemplate  of a GridTemplateColumn to add an arbitrary control that will help you filter your content. Alternatively, you can try to implement filtering for custom columns. If you want to use the built-in filter controls, or fire a filter command event from code, you need to prevent the default filter behavior and implement custom filtering using RadGrid's ItemCommand event:

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.FilterCommandName)
    {
        e.Canceled = true;
        //implement custom filtering here and call e.Item.OwnerTableView.Rebind()
    }
}

Veli
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Coy
Top achievements
Rank 1
answered on 14 Feb 2011, 07:52 PM
Hi Veli,  thanks for the reply!

While working with the filter template, I noticed a behavior that was extremely odd.  Perhaps it has some explanation or perhaps it is a bug?

As stated above, to create my custom filter template, I placed an asp textbox and an asp image button within the filter template like so:

<div style="position:relative; float:left; padding-top:1px"><asp:TextBox ID="txtFilter" Text='<%# hfFilterText.Value %>' runat="server"></asp:TextBox></div>
<div id="filterButton" style="position:relative; float:left; padding-left:3px; padding-top:1px">
    <asp:ImageButton ID="imgFilter" runat="server" OnClientClick="SetFilterTextHF(this);" OnClick="imgFilter_Click" CssClass="telerikFilterBtn" 
    ImageUrl="~/Images/transparent.gif" onmouseover="this.className='telerikFilterBtnMoused'" 
    onmouseout="this.className='telerikFilterBtn'">
    </asp:ImageButton>
</div>

The problem that I found that setting the Text property on the Textbox would never work.  If I set the Text property to Text="hello", the text box always appeared to be empty.  It was as if something was clearing the textbox.  It was making no sense to me and I actually thought I was losing my mind.  Maybe I fat fingered the tag property, everything entered my mind.  I spent at least an hour trying to troubleshoot why the text that I was explicitly setting wasn't displaying in the text box.

One thing that I tried seemed to work.  I added another textbox to the filter template and set it's text proprety to "hello".  So I had 2 asp text box controls with the Text property set to "hello" on both, but then when it rendered, I saw 2 text boxes but only one had text in it.

To get around this, I set the first textbox in the filter template Visible = False, to effectively hide it and the second textbox allowed me to set the Text property.  I ended up with a filter template that looks like this:

<asp:TextBox ID="txtHidden" runat="server" Visible="false" />
                                        <div style="position:relative; float:left; padding-top:1px"><asp:TextBox ID="txtFilter" Text='<%# hfFilterText.Value %>' runat="server"></asp:TextBox></div>
                                        <div id="filterButton" style="position:relative; float:left; padding-left:3px; padding-top:1px">
                                            <asp:ImageButton ID="imgFilter" runat="server" OnClientClick="SetFilterTextHF(this);" OnClick="imgFilter_Click" CssClass="telerikFilterBtn" 
                                            ImageUrl="~/Images/transparent.gif" onmouseover="this.className='telerikFilterBtnMoused'" 
                                            onmouseout="this.className='telerikFilterBtn'">
                                            </asp:ImageButton>
                                        </div>

Has anyone run into this before?  Is there an explanation?  A colleague mentioned that I might have to set the "FilterText" property to make it display within the textbox but I did not try that.

Thanks!

Coy
0
Veli
Telerik team
answered on 16 Feb 2011, 09:24 AM
Hi Coy,

This is probably the GridColumn trying to set the current filter value of the column to the first found TextBox control in the filter cell. When you use the default filtering functionality, every column keeps its own filter value in a column property named CurrentFilterValue. When the filter cell (the column cell in the filter item) databinds, the grid column tries to find the first TextBox control in the Controls collection of the cell. If it does find it, it sets the Text of the textbox to be the CurrentFilterValue. This is why you cannot set the Text of the first TextBox in the filter template  - it is always overwritten by the column. In this respect, using a hidden TextBox control as the first control in the template is feasible. Thus, the column will always set the text of the invisible textbox and you can get the next one display your custom Text.

Greetings,
Veli
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Coy
Top achievements
Rank 1
Answers by
Coy
Top achievements
Rank 1
Veli
Telerik team
Share this question
or