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

[Solved] Change DataFormatString for cell based on another cell

2 Answers 181 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Curtis Hunter
Top achievements
Rank 1
Curtis Hunter asked on 03 Jan 2010, 01:03 AM
I have a situation where one column can have either dollar amounts or plain decimal amounts (these are used as multipliers) and I need to change the DataFormatString for the cell based on the value of another cell in the row. 

We are binding the grid in the OnNeedDataSource event to a IList<>.  I've looked at the ItemDataBound event walk throughs but I can't seem to figure out how to access the DataFormatString.

How can I do this?

Thanks
Curtis


2 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 04 Jan 2010, 12:28 PM
Hello Curtis,

DataFormatString affects the whole column therefore it isn't suitable for this scenario. You can use String.Format to format each cell on ItemDataBound.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        double myValue = (double)DataBinder.Eval(e.Item.DataItem, "myValue");
        GridDataItem item = e.Item as GridDataItem;
 
        if (something)
            item["myColumn"].Text = String.Format("{0:n}", myValue);
        else
            item["myColumn"].Text = String.Format("{0:c}", myValue);
    }
}

I hope this helps.

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Curtis Hunter
Top achievements
Rank 1
answered on 04 Jan 2010, 02:25 PM
Thanks Daniel!

That worked like a charm.  I was so focused on DataFormatString that I didn't even think about just dealing with the .Text directly.

Curtis
Tags
Grid
Asked by
Curtis Hunter
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Curtis Hunter
Top achievements
Rank 1
Share this question
or