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

GridTemplateColumn - replacing the filter editor's TextBox control directly

5 Answers 89 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andy F.
Top achievements
Rank 1
Iron
Andy F. asked on 11 Dec 2014, 03:27 PM
It's a pity that the GridTemplateColumn doesn't support standard filter editors based on either the DataType or DateField data type (e.g., a DateTime data type is rendered as though it was a GridDateTimeColumn filter editor).  This seems like an easy implementation if <FilterTemplate> is not included but filtering is turned on.  Or another idea would be an alternative to <FilterTemplate> that would turn on the standard filter editor and let you set attributes on that.  I really want the filter button without all the extra work.

But maybe this is a quick way around it?  This is the column in the grid:

<telerik:GridTemplateColumn HeaderText="Expires On" DataType="System.DateTime" Visible="true" ShowFilterIcon="true" ShowSortIcon="true" UniqueName="dpExpDate" DataField="EXPIRATION_DATE" AllowFiltering="true" SortExpression="EXPIRATION_DATE" AllowSorting="true" FilterListOptions="VaryByDataType" >
    <HeaderStyle Width="240px" />
    <ItemStyle Width="240px" />
    <ItemTemplate>
        <asp:Label runat="server" ID="lblExpDate" />
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadDateTimePicker runat="server" ID="dpExpDate" Skin="Office2010Blue" Width="220px">
            <DateInput Display="true" DisplayDateFormat="G" DateFormat="G" runat="server" Font-Size="13px" Font-Name="Arial" />
            <TimePopupButton Visible="false" />
        </telerik:RadDateTimePicker><asp:Label runat="server" ID="lblInsertIn" Text="In" />
        <telerik:RadDropDownList runat="server" ID="ddlInsertHours" Font-Names="Arial" Font-Size="13px" Width="80px" Skin="Office2010Blue" />
        <asp:Label runat="server" ID="lblInsertHours" Text="Hours" />
    </EditItemTemplate>
</telerik:GridTemplateColumn>

And here's what I did in grid_ItemCreated:

if (e.Item is GridFilteringItem)
{
    GridFilteringItem item = e.Item as GridFilteringItem;
    if (item["dpExpDate"].Controls[0] is TextBox)
    {
        DateTime value = DateTime.MinValue;
        DateTime.TryParse(gridBatches.MasterTableView.GetColumn("dpExpDate").CurrentFilterValue, out value);
 
        RadDateTimePicker pick = new RadDateTimePicker();
        pick.Width = Unit.Pixel(200);
        pick.DateInput.DateFormat = "G";
        pick.DateInput.DisplayDateFormat = "G";
        pick.TimePopupButton.Visible = false;
        pick.DateInput.Width = Unit.Pixel(180);
        pick.Skin = "Office2010Blue";
        pick.DateInput.Font.Size = FontUnit.Parse("13px");
        pick.DateInput.Font.Name = "Arial";
        if (!value.Equals(DateTime.MinValue)) pick.DbSelectedDate = value;
 
        item["dpExpDate"].Controls.RemoveAt(0);
        item["dpExpDate"].Controls.AddAt(0, pick);
    }
}

Any reason I can't do that?  It seems to filter perfectly and appears to fit in with the Telerik examples that just assume there's always TextBox in Controls[0] anyway.

Feedback?

5 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 16 Dec 2014, 08:24 AM
Hi Andy,

Please check out the following help article which elaborates more on applying a built-in filter to TemplateColumns. I hope this information 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.

 
0
Andy F.
Top achievements
Rank 1
Iron
answered on 16 Dec 2014, 11:28 AM
I had already read all the documents on how to create my own template for filters, that's not what I always want to do, especially when the standard filter editors are more appealing in some situations... but the standard filter editor for the templatecolumn is always a textbox and not based on the data type of the column's data, so I went looking for a way around that and hit on something that may or may not be "legal" (although it seems to work great).  So the question is: any reason I can't do what I did?
0
Konstantin Dikov
Telerik team
answered on 19 Dec 2014, 09:30 AM
Hello Andy,

I cannot see any reason for not using this approach, especially when it is working correctly.

However, if you need more generic solution, you may have to switch to programmatic creation of the grid and its columns and specify a predefined FilterTemplate, depending on the data type of the field.

In the following help article you can find how to create RadGrid programmatically:

Best Regards,
Konstantin Dikov
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
Andy F.
Top achievements
Rank 1
Iron
answered on 19 Dec 2014, 12:31 PM
The reason I went down this path instead of all the additional programming is that I wanted a simple way to get one of the standard filter editors along with the filter menu into a template column's filter item.  All the published examples I've seen show how to create a template column filter without the menu or they show a very complex do-it-all-yourself approach to managing the filter menu and I was looking for a short-cut to get to something that's already somewhere in the RadGrid code library.
0
Kostadin
Telerik team
answered on 24 Dec 2014, 08:35 AM
Hi Andy,

I am afraid there isn't a code library which demonstrates the scenario which you are trying to achieve. If you want a different control to be rendered based on the data filed type you can manually create your filter template by following the examples in our documentation or the previously provided suggestion.

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
Andy F.
Top achievements
Rank 1
Iron
Answers by
Kostadin
Telerik team
Andy F.
Top achievements
Rank 1
Iron
Konstantin Dikov
Telerik team
Share this question
or