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

RadGrid Calculated Column

1 Answer 389 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Silviu
Top achievements
Rank 1
Silviu asked on 02 Jun 2015, 12:15 PM

var results = from t in context
      where t.Name == userName
      select t;
 
RadGrid1.DataSource = results;
RadGrid1.DataBind();

I have a RadGRid which is populated via code behind with data from an Odata endpoint, as above.

Some of the fields returned contain dates.

I need to create a calculated column which subtracts the current from the date in one of the bound field and returns a value in days.

How can that be performed from code behind and in what event?

 

 I can create the column as below but not sure how to setup the Expression attribute.

GridCalculatedColumn gcc = new GridCalculatedColumn();
gcc.DataFields = new string[] { "CreatedDate" };
gcc.Expression = ???
RadGrid1.MasterTableView.Columns.AddAt(1, gcc);

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 05 Jun 2015, 10:04 AM
Hello Silviu,

I have examined the code snippets and noticed that DataBind() is used to populate RadGrid with data. Note that DataBind() is used only for simple data binding. As the name implies, this type of binding is suitable only for the most simple scenarios.

It is recommended to use advanced data binding. You can handle the NeedDataSource event of RadGrid and set the DataSource property there. Note that you should not use DataBind() in this case. Check out the following article that describe the approach in more detail.


In order to subtract the dates you can use the following code:


GridCalculatedColumn gcc = new GridCalculatedColumn();
gcc.DataType = typeof(System.TimeSpan);
gcc.DataFields = new string[] { "CreatedDate"};
gcc.Expression = "{0}.Value.Subtract(DateTime.Now)";
gcc.UniqueName = "CalculatedColumnName";
 
grid.MasterTableView.Columns.Add(gcc);



Regards,
Viktor Tachev
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Silviu
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or