RadGrid Calculated Column

2 posts, 0 answers
  1. Silviu
    Silviu avatar
    63 posts
    Member since:
    Feb 2015

    Posted 02 Jun 2015 Link to this post

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

    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);
  2. Viktor Tachev
    Viktor Tachev avatar
    2396 posts

    Posted 05 Jun 2015 Link to this post

    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";

    Viktor Tachev
    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
Back to Top