Solved

Formatting dynamic Gridview for decimal data type

Posted on 2011-02-11
7
1,127 Views
Last Modified: 2012-05-11
Hi Experts,
I am attempting to format a Gridview at runtime that returns Decimal values but shows 4 decimal places whereas I want  to either show 2 decimal places or use {c} or "{0:c}" to get a currency format.
What am I doing wrong please?
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string text = e.Row.Cells[3].Text;
            e.Row.Cells[3].Text = Server.HtmlDecode(text);
            text = string.Format("{0:c}", text);
        }

Open in new window

0
Comment
Question by:madkrell
  • 5
7 Comments
 
LVL 9

Expert Comment

by:s_chilkury
ID: 34868901
Try this:
"{0:n2}"
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 34870468
Hi,

You have to first check the datatype

then as per datatype you have to change the CellStyle properties.
0
 

Author Comment

by:madkrell
ID: 34871524
samirbhogayta...  Can you explain what you mean by providing an example?  I already know the datatype that's returned?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:madkrell
ID: 34871607
s_chilkury... Thanks but your suggestion still has no affect in the Gridview.
0
 

Author Comment

by:madkrell
ID: 34877922
I've also tried this but still no change?
if (e.Row.RowType == DataControlRowType.DataRow )
                    {
                    try
                    {
                    string name = e.Row.Cells[0].Text.ToString();
                    e.Row.Cells[1].Attributes.Add("Style", "{0:c}");
                    e.Row.Cells[2].Attributes.Add("Style", "{0:c}");
                    e.Row.Cells[3].Attributes.Add("Style", "{0:c}");
                    e.Row.Cells[4].Attributes.Add("Style", "{0:c}");
                    e.Row.Cells[5].Attributes.Add("Style", "{0:c}");
                    ...
                    }
                    catch { }
                    } 

Open in new window

0
 

Accepted Solution

by:
madkrell earned 0 total points
ID: 34881917
After lots of wasted time on what should be a simple thing I solved by doing this:
Decimal result;
                        for (int i = 0; i < GridView2.Rows.Count; i++)
                        {
                            foreach (TableCell c in GridView2.Rows[i].Cells)
                          {
                            if (Decimal.TryParse(c.Text, out result))
                            {
                                c.Text = String.Format("{0:c}", result);
                            }
                          }
                        }

Open in new window

0
 

Author Closing Comment

by:madkrell
ID: 34924177
Thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
"Emulate" TAB key when press Enter Key 3 45
Add a Condition in ASP Repeater 6 34
linq, c# 8 22
vb.net convert long time to mm:ss 23 11
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question