Solved

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

Posted on 2010-08-31
3
1,030 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
ID: 33571405
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
ID: 33572549
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
ID: 33576660
Exactly what I needed. Thank you.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

867 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

16 Experts available now in Live!

Get 1:1 Help Now