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

[Solved] Sorting a column with value calculated in ItemDataBound

1 Answer 245 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stuart
Top achievements
Rank 1
Stuart asked on 30 Jun 2009, 11:22 AM
Hi,

I've got a column which dispalys the number of days an item has been in stock. I've implemented this as as GridTemplateColumn with a label for the ItemTemplate. I couldn't use a GridCalculatedColumn as this does not work with dates. The code in the ItemDataBound event is as follows:

 

if (e.Item is GridDataItem)

 

{

 

GridDataItem dataItem = e.Item as GridDataItem;

 

 

Label label = (Label)dataItem.FindControl("DaysInStock");

 

label.Text =

DateTime.Now.Subtract(DateTime.Parse(dataItem["DateIntoStock"].Text)).Days.ToString();

 

}

I also want to be able to sort on this column. How can I implement this?

Thanks,

Stuart

1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 03 Jul 2009, 06:44 AM

Hello Stuart,

Thank you for contacting us and for your question.
Basically, the grid control sorts and filters its data on the database level. That is, if you alter the values in some other event, for example ItemDataBound, or PreRender, the control cannot be aware of this change, until it is persisted in the database.

Another option in this case would be to use a calculated column. This is demonstrated in the code sample attached to this message. Please, take a look at it and let me know how it goes.

Regards,

Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Stuart
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or