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

Set Expression depending on if DataValue is null

2 Answers 54 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 15 May 2013, 06:34 PM

Right now, I have two GridBoundColumns pulling OfficePhone and Extension from a SQL database.

<telerik:GridBoundColumn HeaderText="Phone" DataField="OfficePhone" UniqueName="OfficePhone">
</
telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="Ext" DataField="Extension" UniqueName="Extension">
</
telerik:GridBoundColumn>

I would like to combine the two columns into one, into something like this:

<telerik:GridCalculatedColumn HeaderText="Phone" UniqueName="Phone" DataType="System.String" DataFields="OfficePhone, Extension" Expression="{0} + ' ext. ' + {1})">
</
telerik:GridCalculatedColumn>

but extension is usually null or "", causing most of the cells to go blank.

Is there a way to set the expression programmatically, depending on whether there is a value for extension?

2 Answers, 1 is accepted

Sort by
0
Matthew
Top achievements
Rank 1
answered on 16 May 2013, 08:41 PM
I was able to figure it out using ClientDataKeyNames and ItemDataBound.

<MasterTableView AutoGenerateColumns="false" DataKeyNames="ID" Name="Roster" HierarchyLoadMode="Client" GroupLoadMode="Client"
ClientDataKeyNames="ID,OfficePhone,Extension">

And in the .cs:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            string strOfficePhone = dataItem.GetDataKeyValue("OfficePhone").ToString();
            string strExtension = dataItem.GetDataKeyValue("Extension").ToString();
            if (strExtension == null || strExtension == String.Empty)
            {
                dataItem["Phone"].Text = strOfficePhone;
            }
            else
            {
                dataItem["Phone"].Text = strOfficePhone + " ext. " + strExtension;
            }
        }
    }
0
Andrey
Telerik team
answered on 20 May 2013, 12:08 PM
Hi,

Thank you for contacting us.

Yes, this is the approach I would suggest, however, I would recommend to use the DataKeyNames collection instead of the ClientDataKeyNames collection, because the ClientDatakeyNames collection is serialized to the client since it is mainly used for accessing a cell value on the client-side with JavaScript.

Other than that your approach looks OK. Let me know if you have further questions.

Kind regards,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Matthew
Top achievements
Rank 1
Answers by
Matthew
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or