Setting the format of an item in the ItemDataBound event

4 posts, 0 answers
  1. Bill
    Bill avatar
    175 posts
    Member since:
    Apr 2010

    Posted 23 Jan 2013 Link to this post

    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}";
            }
        }
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 23 Jan 2013 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Bill
    Bill avatar
    175 posts
    Member since:
    Apr 2010

    Posted 24 Jan 2013 Link to this post

    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>
  5. Bill
    Bill avatar
    175 posts
    Member since:
    Apr 2010

    Posted 24 Jan 2013 Link to this post

    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}";
            }
Back to Top