?
Solved

Formatting dynamic Gridview for decimal data type

Posted on 2011-02-11
7
Medium Priority
?
1,155 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

831 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