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

Grid Column format as Currency

2 Answers 1654 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 12 Oct 2012, 08:48 PM
Having trouble getting a radgrid column to format as $1,234.00; it always displays as 1234.  The database field is Double. The code is as follows:
       <telerik:RadGrid ID="RadGrid1" runat="server" style="margin-top:25px;"
AutoGenerateColumns="true" oncolumncreated="RadGrid1_ColumnCreated">
           <MasterTableView  TableLayout="Fixed" />
           <ClientSettings EnableRowHoverStyle="true" >
               <Selecting AllowRowSelect="False"/>
               <ClientEvents />
               <Scrolling AllowScroll="True"/>
               <Resizing AllowColumnResize="true" AllowResizeToFit="true" ResizeGridOnColumnResize="true" />
           </ClientSettings>
       </telerik:RadGrid>

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using myNameSpace.BusinessLogic.myLogic;
 
namespace myNameSpace
{
    public partial class myScreen : System.Web.UI.UserControl
    {
        private MyController myController = new MyController();
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            List<Guid> thisPositionIds = myController.GetPositionIDs();
            // add 1st column, the Item
            dt.Columns.Add("Item");
 
            // make the Annual Costs columns
            foreach (Guid positionId in thisPositionIds)
            {
                // add a column
                dt.Columns.Add(new DataColumn(myController.GetPositionName(positionId)
                    + System.Environment.NewLine + "Annual Costs",
                    System.Type.GetType("System.Decimal")));
            }
 
            // get the data for the rows
            List<Guid> thisItemIds = myController.GetItemIDs();
            foreach (Guid itemID in thisItemIds)
            {
                DataRow dr = dt.NewRow();
 
                // enter the first column, the Item Name
                dr["Item"] = myController.GetItemName(itemID);
 
                // enter the final set of columns, the Annual Costs
                foreach (Guid positionId in thisPositionIds)
                {
                    dr[myController.GetPositionName(positionId) + System.Environment.NewLine + "Annual Costs"]
                        = myController.GetAnnualCosts(itemID, positionId);
                }
                dt.Rows.Add(dr);
            }
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }
 
        protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
        {
            if (e.Column.ColumnType == "GridBoundColumn")
            {
                // get column header text
                string colName = e.Column.UniqueName;
 
                // get last 12 chars of text (if it is long enough)
                string colNameEnd;
                if (colName.Length >= 12)
                    colNameEnd = colName.Substring(colName.Length - 12, 12);
                else
                    colNameEnd = "x";
                // format according to which column this
                if (colName == "Item")
                    e.Column.HeaderStyle.Width = Unit.Pixel(80);
                else if (colNameEnd == "Annual Costs")
                {
                    (e.Column as GridBoundColumn).DataFormatString = "{0:C}";
                }
            }
        }
    }
}

Any hints as to what I'm missing would be appreciated.
-Scott

2 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 14 Oct 2012, 07:19 PM
Hello,

protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
       {
           if()// put your condition here
           {
               (e.Column as GridBoundColumn).DataFormatString =  "{0:$###,###.##}";
           }
       }


Thanks,
Jayesh Goyani
0
Scott
Top achievements
Rank 1
answered on 15 Oct 2012, 04:29 PM
Jayesh, thanks for your suggestion, but turns out the issue wasn't 'standard' vs 'custom' formatting, rather that the column type was changed by declaring it to be Decimal.  The correct code turns out to be:
protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
 {
     if ((e.Column.ColumnType == "GridBoundColumn") && (e.Column.UniqueName == "Item"))
             e.Column.HeaderStyle.Width = Unit.Pixel(80);
     elseif (e.Column.ColumnType == "GridNumericColumn")
     {
         // get column header text
         string colName = e.Column.UniqueName;
 
        // check last 12 chars of text (if it is long enough) to see if this is an Annual Costs column
        if ((colName.Length >= 12) && (colName.Substring(colName.Length - 12, 12) == "Annual Costs"))
            (e.Column as GridNumericColumn).DataFormatString = "{0:C}";
     }
 }

Thanks,
-Scott
Tags
Grid
Asked by
Scott
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Scott
Top achievements
Rank 1
Share this question
or