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

GridTemplateColumn Filter issue

4 Answers 115 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Greg Stevens
Top achievements
Rank 1
Greg Stevens asked on 28 Mar 2011, 07:28 PM
Hello, I'm using a gridtemplatecolumn to find the differnce betwean two date columns. I then want to take the average of that column which I do through code. It works fine until a filter is applied on any column. I've have the code shown below. Any suggestions?

<

 

 

telerik:GridDateTimeColumn UniqueName="DatePlaced" DataField="DatePlaced" HeaderText="Date Placed"

 

 

 

 

HeaderStyle-Width="160px" DataFormatString="{0:d}">

 

 

 

</telerik:GridDateTimeColumn>

 

 

 

<telerik:GridDateTimeColumn UniqueName="DateShipped" DataField="DateShipped" HeaderText="Date Shipped"

 

 

 

 

HeaderStyle-Width="160px" DataFormatString="{0:d}">

 

 

 

</telerik:GridDateTimeColumn>

 

 

 

<telerik:GridTemplateColumn DataField="DaysToShip" HeaderText="Days To Ship" >

 

 

 

<ItemTemplate>

 

 

 

<asp:Label ID="lblDaysToShip" runat="server"></asp:Label>

 

 

 

</ItemTemplate>

 

 

 

<FooterTemplate>

 

 

 

<asp:Label ID="lblDaysToShipFooter" runat="server"></asp:Label>

 

 

 

</FooterTemplate>

 

 

 

</telerik:GridTemplateColumn>


C# code

 

 

double

 

 

totalRows, countRows;

 

 

protected

 

 

 

void TrackingGrid_ItemDataBound(object source, GridItemEventArgs e)

 

 

{

 

 

 

 

if (e.Item is GridDataItem)

 

 

{

 

 

 

 

GridDataItem item = (GridDataItem)e.Item;

 

 

(item.FindControl(

 

 

"lblDaysToShip") as Label).Text = DateTime.Parse(item["DateShipped"].Text).Subtract(DateTime.Parse(item["DatePlaced"].Text)).Days.ToString();

 

 

totalRows +=

 

 

Double.Parse((item.FindControl("lblDaysToShip") as Label).Text);

 

 

countRows += 1;

 

}

 

 

 

 

if (e.Item is GridFooterItem)

 

 

{

 

 

 

 

GridFooterItem footerItem = e.Item as GridFooterItem;

 

 

(footerItem.FindControl(

 

 

"lblDaysToShipFooter") as Label).Text = "Average days: " + (totalRows / countRows).ToString("N2");

 

 

}

 

}


Thanks for your help
Greg

4 Answers, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 01 Apr 2011, 07:42 AM
Hello Greg,

Your code seems fine at first look. Could you please tell what exactly goes wrong when you try to filter? Is it an exception or the grid does not produce the required result?

Best wishes,
Tsvetina
the Telerik team
0
Greg Stevens
Top achievements
Rank 1
answered on 01 Apr 2011, 06:03 PM
The code works great if no filter is applied on any of the columns. Running it in debug mode, when I apply a filter (in this case an unrelated column) it still fires the ItemDataBound function for rows that are eventually getting filtered out. So it's throwing the numbers off. I looked through the variables but couldn't find anything that flagged the rows to filter them out.
Thanks for your help,
Greg
0
Tsvetina
Telerik team
answered on 06 Apr 2011, 02:51 PM
Hi Greg,

Unless, for some reason, RadGrid is rebound before the filter command, the ItemDataBound event will be fired after the filtering has taken place, as listed in our RadGrid Event Sequence article.

This would be different only if your grid ViewState is turned off. If this is so, you can check in ItemDataBound if the grid has already been filtered. This is achieved by checking its RadGrid.MasterTableView.FilterExpression property. If it is an empty string, then no filter has been applied yet.

Best wishes,
Tsvetina
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Greg
Top achievements
Rank 1
answered on 08 Apr 2011, 10:04 PM
Using the FilterExpression command I'm getting a better idea what is happening. When I apply a filter to a column, it first run the filter I removed then it runs the filter I applied. So I was able to fix it by telling it to ignore the first filter by using the filter expression.
Thanks
Greg
Tags
Grid
Asked by
Greg Stevens
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Greg Stevens
Top achievements
Rank 1
Greg
Top achievements
Rank 1
Share this question
or