Solved

Formatting dynamic Gridview for decimal data type

Posted on 2011-02-11
7
1,123 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

24 Experts available now in Live!

Get 1:1 Help Now