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

[Solved] Cast GridCalculatedColumn to change expression

3 Answers 219 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pierre
Top achievements
Rank 1
Pierre asked on 10 Feb 2010, 11:31 PM
Hi,

I need to change expression based on server side value for a declarative grid. How to cast the GridCalculatedColumn OnItemDataBound to modify the expression values ?. Need to be edited?
 
Thanks.

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 11 Feb 2010, 07:22 AM
Hello,

I am not sure whether it is possible to change the Expression of GridCalculatedColumn for each row in the grid based on cell value. One suggestion to achieve the same functionality is by adding GridTemplateColumn with Label control placed inside and setting the Text property for the label after calculating the result explicitly in ItemDataBound event.

-Shinu.
0
Pierre
Top achievements
Rank 1
answered on 11 Feb 2010, 12:47 PM
Hi Shinu,

Normally i may use this feature changing runtime the columns for declarative columns but some reason i can't cast "AS"

Sample
 protected void RadGrid1_ColumnCreated(object  sender, Telerik.Web.UI.GridColumnCreatedEventArgs e) 
  if(e.Column.UniqueName == "BirthDate") 
  { 
   GridBoundColumn boundColumn = e.Column as GridBoundColumn; 
   boundColumn.ReadOnly = true
   boundColumn.DataFormatString = "{0:d}"
  } 
}  
my
protected void PlanGrid_PreRender(object sender, System.EventArgs e) 
    { 
        foreach (GridColumn column in PlanGrid.Columns) 
        { 
            if (column.UniqueName == "PriceUs") 
            { 
                ((GridCalculatedColumn)column).Expression = "{0}/" + RUNTIMEVALUE; 
                break; 
            } 
        } 
        PlanGrid.Rebind(); 
    }  

error :No property or field 'as' exists in type 'DataRowView'

Thanks
0
Radoslav
Telerik team
answered on 16 Feb 2010, 03:16 PM
Hello Pierre,

I am afraid the behavior you are trying to attain is not possible. However, to achieve your goal you could use the ItemDataBound event. When it is raised you can perform the required calculation and then apply the modified text to the CalculatedColumn cell:

void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        item["TotalCost"].Text = Convert.ToDecimal(item["minPrice"].Text) + RUNTIMEVALUE;
    }
}

To avoid the error with message "No property or field 'as' exists in type 'DataRowView'"  you need to set the Expression property of the CalculatedColumn column:

<telerik:GridCalculatedColumn HeaderText="Total Cost" UniqueName="TotalCost" DataType="System.Decimal" DataFields="minPrice, maxPrice" Expression="{0}+{1}" />

Also you could check out the following online documentation article which explains the RadGrid's column types:

http://www.telerik.com/help/aspnet-ajax/grdcolumntypes.html
http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/calculatedcolumns/defaultcs.aspx

I hope this helps.

Regards,
Radoslav
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.
Tags
Grid
Asked by
Pierre
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Pierre
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or