Solved

asp.net, vb - how to format number in unbound gridview

Posted on 2010-08-31
3
1,027 Views
Last Modified: 2012-05-10
In a asp.net page, I'm populating a GridView in code by filling a DataSet, then binding the GridView's DataSource property to the DataSet (using SQL).

Some of my number columns need to be displayed using 2 decimal places.

In my sql query I made sure I rounded my numbers displayed 2 decimal places (Round(field, 2, 1)).

But when I run my code, 4 decimal places are shown.

So, how do I format numbers in an unbound gridview (in asp.net, vb) to show 2 decimal places?
0
Comment
Question by:Rick
3 Comments
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
Try something like this:

        Dim myFieldData As String = "10.3234"

        Dim strText As String = CDbl(myFieldData).ToString("#.00")

Open in new window

0
 
LVL 30

Accepted Solution

by:
MlandaT earned 500 total points
Comment Utility
The problem is that those columns are numeric in the dataset and when displayed in the datagrid it uses that funny format.

Option 1: In your SQL Query, CAST the column into a varchar... check this: SELECT CAST(CAST(123.4556 AS decimal(6,2)) AS varchar). That way, in the dataset, the column is of string type and will be presented 'as is'

Option 2: Loop through the columns after databinding and perhaps call a function that formats the columns individually.. something like:

private string setFormating(DataColumn bc)
    {
      string dataType = null;
      switch(bc.DataType.ToString())      
      {      
        case "System.Int32":  
          dataType = "{0:#,###}";
         break;                
        case "System.Decimal":            
          dataType =  "{0:c}";
          break;
        case "System.DateTime":
         dataType="{0:dd-mm-yyyy}";
         break;
        case "System.String":
          dataType="";
          break;
        default:    
          dataType = "";
          break;      
      }
      return dataType;
    }
0
 
LVL 13

Author Closing Comment

by:Rick
Comment Utility
Exactly what I needed. Thank you.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

772 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

15 Experts available now in Live!

Get 1:1 Help Now