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

Cannot get DataFormatString to show currency

8 Answers 1454 Views
Grid
This is a migrated thread and some comments may be shown as answers.
KevinBlount
Top achievements
Rank 1
KevinBlount asked on 02 Jul 2010, 02:34 PM
Whatever I do (and I've tried many ideas already given in these forums) i cannot get my columns to show currencies, just plain numeric values.

I want my grid to show the "buyprice" and totalcost columns as "{0:C}" or "{0:$###.##}"

Here's my code:

.aspx
<telerik:RadGrid  
        ID="RadGrid1" runat="server" 
        Width="95%" 
        OnNeedDataSource="RadGrid1_NeedDataSource" 
        > 
        <MasterTableView AutoGenerateColumns="false"
            <Columns> 
                <telerik:GridBoundColumn DataField="id" Visible="false" ReadOnly="true"></telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="symbol" HeaderText="Symbol"  ReadOnly="true" ></telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="buyprice" HeaderText="Buy Price" DataType="System.Decimal" DataFormatString="{0:C}" ></telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="totalcost" HeaderText="Total Cost" DataType="System.Decimal" DataFormatString="{0:C}" ></telerik:GridBoundColumn> 
                <telerik:GridButtonColumn  ConfirmText="Are you sure?" ConfirmDialogType="Classic" ButtonType="ImageButton" ImageUrl="/images/delete.gif"CommandName="Delete"> 
                </telerik:GridButtonColumn> 
            </Columns> 
        </MasterTableView> 
    </telerik:RadGrid> 

.aspx.cs
        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
        { 
            string XMLFilePath = getXMLPath(); 
 
            DataSet ds = new DataSet(); 
            ds.ReadXml(XMLFilePath); 
 
            DataView dv = new DataView(ds.Tables[0]); 
            dv.Sort = "datecalculated DESC"
 
            RadGrid1.DataSource = dv
             
        } 

.xml
<?xml version="1.0" encoding="utf-8"?> 
<calculations> 
  <calculation id="1" symbol="SYM1" buyprice="3.4" totalcost="831.483" datecalculated="06/30/2010" /> 
  <calculation id="2" symbol="SYM2" buyprice="10.74" totalcost="1088" datecalculated="06/17/2010" /> 
</calculations> 

am I missing anything? Is my method of populating RadGrid1 causing this, can string formatting not be applied to boundcolumns? (I did try using GridNumericColumn, still without success).

8 Answers, 1 is accepted

Sort by
0
Casey
Top achievements
Rank 1
answered on 02 Jul 2010, 04:23 PM
Hi Kevin,

I believe if you place the $ outside of the {} it will work. Like: ${0:###.##}.

Hope this helps,
Casey
0
KevinBlount
Top achievements
Rank 1
answered on 02 Jul 2010, 04:35 PM
Hi Casey - thanks for the reply

I just gave that a try, and while it does indeed at the $ symbol, the formatting of the number still isn't working correctly.

for example, I updated my .aspx to include:
<telerik:GridBoundColumn DataField="totalcost" HeaderText="Total Cost" DataType="System.Decimal" DataFormatString="${0:#.##}" ></telerik:GridBoundColumn>    
                   
 

What I believe should happens it that an XML value of '831.483' (as used in the first record in my XML file above) should be formatted as '$831.48', but it's still just '$831.483' (i.e. the 3rd decimal place is still visible)

I'm wondering if this to do with the values I wanted to put in each column being strings?

Any ideas?
0
Casey
Top achievements
Rank 1
answered on 02 Jul 2010, 04:39 PM
Kevin,

Try: DataFormatString="{0:C2}"

Casey
0
KevinBlount
Top achievements
Rank 1
answered on 02 Jul 2010, 04:51 PM
Hi again,

I tried that too (previously and again just now)

My XML value of "1088" still comes out without any cents, i.e. "${0:C2}" renders as "$1088", and just "{0:C2}" is rendered simply as "1088"
0
Casey
Top achievements
Rank 1
answered on 02 Jul 2010, 05:05 PM
What version of the Telerik controls are you using? {0:C2} works with my version (2010.1.309.35). It displays the $ and 2 decimals when there are none with my version. I'm not sure why it won't work for you. I also use an ObjectDataSource as my datasource for my grid, but I don't see how that could make a difference.

Hopefully somebody will be able to shed some light on the situation.

Sorry I couldn't help resolve this.
0
KevinBlount
Top achievements
Rank 1
answered on 02 Jul 2010, 05:35 PM
I'm using 2009.2.826.35 - a little behind, but not much. I've not had time to upgrade to 2010 yet, but according to the 2009 documentation, the DataFormatString should still work.

Thanks for trying to help - I hope someone from Telerik checks this thread, especially as there's no marked answer.
0
KevinBlount
Top achievements
Rank 1
answered on 02 Jul 2010, 09:20 PM
Success! (of sorts )

I did some more investigation into my query about 'I'm wondering if this to do with the values I wanted to put in each column being strings?' - and it turns out that Yes, the source column type does play a big part.

So, in my code I create my DataSet by reading the XML file. I now also create a DataTable, with each column specified as either 'string', 'decimal' or 'datatime'. I then loop through the DataSet, looks for specific columns, convert the values as required - e.g. "decimal newRowBuyPrice = Convert.ToDecimal(row["buyprice"].ToString());" - and create new a new Row. Finally I add those new rows to my DataTable and construct my DataView from the DataTable instead of the DataSet.

Now, because the source value for a column is decimal the DataFormatString="{0:C}" works as required.

phew!

incase anyone wants to see it, here's my new NewDataSource method:

protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
        {  
            string XMLFilePath = getXMLPath();  
 
            DataSet ds = new DataSet();  
            ds.ReadXml(XMLFilePath);  
 
            DataTable newdt = new DataTable();  
 
            foreach (DataColumn col in ds.Tables["calculation"].Columns)  
            {  
                switch (col.ColumnName)  
                {  
                    case "buyprice":  
                    case "totalcost":  
                        newdt.Columns.Add(col.ColumnName, typeof(System.Decimal));  
                        break;  
 
                    case "symbol":  
                        newdt.Columns.Add(col.ColumnName, typeof(System.String));  
                        break;  
 
                    case "datecalculated":  
                        newdt.Columns.Add(col.ColumnName, typeof(System.DateTime));  
                        break;  
                }                  
            }  
 
            foreach (DataRow row in ds.Tables["calculation"].Rows)  
            {  
                string newRowSymbol = row["symbol"].ToString();  
                decimal newRowBuyPrice = Convert.ToDecimal(row["buyprice"].ToString());  
                decimal newRowTotalCost = Convert.ToDecimal(row["totalcost"].ToString());  
                DateTime newRowDateCalculated = Convert.ToDateTime(row["datecalculated"].ToString());  
 
                newdt.Rows.Add(newRowSymbol, newRowBuyPrice, newRowTotalCost, newRowDateCalculated);  
            }  
 
            DataView dv = new DataView(newdt);  
            dv.Sort = "datecalculated DESC";  
 
            RadGrid1.DataSource = dv;  
              
        } 

There may be an easier way to achieve this goal, but as the XML file can only contain strings (afaik), this seems to be as good a way as any to get what I need.


comments welcome, of course.
0
Jr
Top achievements
Rank 1
answered on 02 Mar 2012, 06:57 AM
Try this? 
...
<telerik:GridBoundColumn HtmlEncode="false" ...  DataType="System.Decimal" DataFormatString="${0:#.00}"/>
...
Tags
Grid
Asked by
KevinBlount
Top achievements
Rank 1
Answers by
Casey
Top achievements
Rank 1
KevinBlount
Top achievements
Rank 1
Jr
Top achievements
Rank 1
Share this question
or