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

How to Use GridCalculatedColumn to Display Either a Text or a Decimal Value?

3 Answers 184 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pooya
Top achievements
Rank 1
Pooya asked on 20 Sep 2011, 04:44 PM
I have the below GridCalculatedColumn object and it's being added to the GridTableView but it fails at runtime:

var calculatedColumn = new GridCalculatedColumn
            {
                DataFields = "Volume",               
                Expression = "iif({0}=null,'NA',{0}*100)",
                DataFormatString = "{0:0.00}%",               
            };

What is required to be done is that if the "Volume" field is null, it should display "NA" text otherwise it should display the Volume in 0:0.00 format.

How to achieve this with GridCalculatedColumn?

I'm getting this error:
Telerik.Web.UI.ParseException: Neither of the types 'String' and 'Nullable`1' converts to the other.

The data type of {0} i think is Nullable<decimal>.

Another try:

I removed the DataFormatString and put this expression:
iif({0}.HasValue, Convert.ToString({0}.Value), "NA")

and it worked but it just shows data in a different format e.g. it displays as 100.40 rather than 100.40%.

How to apply formatting inside the expression?

I used String.Format() but it gave an error message.

 Thanks guys,

3 Answers, 1 is accepted

Sort by
0
O'Man
Top achievements
Rank 1
answered on 23 Sep 2011, 02:47 PM
Hi This Oman

I have only small experince but not seen percentege formating on "NA" string. I dont think your idea good.
Maybe you can try set column text in code like in itemDatabound event.

Tanks,
Oman
0
Pooya
Top achievements
Rank 1
answered on 23 Sep 2011, 02:52 PM
very good point; thanks!
0
Pooya
Top achievements
Rank 1
answered on 23 Sep 2011, 04:46 PM
I wrote the below code to see how it works. For BoundColumn columns, the cell.Text has value, for GridCalcualtedColumn columns, cell.Text is always empty which suggests the GridCalculatedColumns are added after the BoundColumns are added and after the ItemDataBound event is raised so this makes ItemDataBound not a possible solution?

  private void RadGridItemDataBound(object sender, GridItemEventArgs e)
        {
            var itemType = e.Item.ItemType;
            switch (itemType)
            {
                case GridItemType.Item:
                case GridItemType.AlternatingItem:
                    {
                        // this item represents a row in the radgrid tables
                        var item = e.Item as GridDataItem;
                        if (item == null) return;
                        foreach (TableCell cell in item.Cells)
                        {
                            if (cell.Text == null)
                            {
                                cell.Text = "null";
                            }
                            if (cell.Text == String.Empty)
                            {
                                cell.Text = "empt";
                            }
                        }
                        
                        break;
                    }
                default:
                    break;
            }
        }

Tags
Grid
Asked by
Pooya
Top achievements
Rank 1
Answers by
O'Man
Top achievements
Rank 1
Pooya
Top achievements
Rank 1
Share this question
or