Solved

Formatting dynamic Gridview for decimal data type

Posted on 2011-02-11
7
1,122 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
Comment Utility
Try this:
"{0:n2}"
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
Comment Utility
Hi,

You have to first check the datatype

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

Author Comment

by:madkrell
Comment Utility
samirbhogayta...  Can you explain what you mean by providing an example?  I already know the datatype that's returned?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

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

Author Comment

by:madkrell
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now