Solved

Formatting dynamic Gridview for decimal data type

Posted on 2011-02-11
7
1,133 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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