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

Setting the format of an item in the ItemDataBound event

3 Answers 680 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bill
Top achievements
Rank 2
Bill asked on 23 Jan 2013, 11:01 PM
I want to be able to set an item to a currency or date format depending upon its name.

Something like the following, but for the ItemDataBound event, not the ColumnCreated event.

How would I be able to accomplish that?
protected void RadGrid1_ColumnCreated(object sender, Telerik.Web.UI.GridColumnCreatedEventArgs e)
    {
        if (e.Column is GridBoundColumn && (e.Column as GridBoundColumn).DataField == "Sales")
        {
            (e.Column as GridBoundColumn).DataFormatString = "{0:C}";
        }
    }

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 24 Jan 2013, 04:41 AM
Hi,

Try the following code to achieve your scenario.
C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem dataItem = (GridDataItem)e.Item;
           double dbl = Double.Parse(dataItem["Uniquename"].Text.ToString());
           string str = String.Format("{0:c}", dbl);
           dataItem["Uniquename"].Text = str;
       }
   }

Thanks,
Shinu
0
Bill
Top achievements
Rank 2
answered on 24 Jan 2013, 02:33 PM
Shinu, using the UniqueName won't work in my case because I have the following defintiion set up for the GridView without any field names defined.

Because of this, I get the following error:
Cannot find a cell bound to column name 'Uniquename'

Is there another way I can check for the field name?

The fields get populated in my code behind via the following code:
RadGrid1.DataSource = dsRpt.Tables[0].DefaultView;
        RadGrid1.DataBind();

<telerik:RadGrid ID="RadGrid1" runat="server"
                        PageSize="20" AllowSorting="True" AllowPaging="True" ShowGroupPanel="True"
                        OnColumnCreated="RadGrid1_ColumnCreated" OnItemDataBound="RadGrid1_ItemDataBound">
                        <PagerStyle Mode="NumericPages"></PagerStyle>
                        <MasterTableView Width="100%">
                        </MasterTableView>
                        <ClientSettings>
                            <Resizing AllowColumnResize="True"></Resizing>
                        </ClientSettings>
                    </telerik:RadGrid>
0
Bill
Top achievements
Rank 2
answered on 24 Jan 2013, 04:02 PM
Shinu, nevermind.... I got the desired result I needed from the following code I put in...

protected void RadGrid1_ColumnCreated(object sender, Telerik.Web.UI.GridColumnCreatedEventArgs e)
    {
        if (e.Column is GridBoundColumn && (e.Column as GridBoundColumn).DataField == "Date")
        {
            (e.Column as GridBoundColumn).DataFormatString = "{0:M/d/yyyy}";
        }
 
        if (e.Column is GridBoundColumn && (e.Column as GridBoundColumn).DataField == "Product1Cash")
        {
            (e.Column as GridBoundColumn).DataFormatString = "{0:C}";
        }
Tags
Grid
Asked by
Bill
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Bill
Top achievements
Rank 2
Share this question
or