Solved

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

Posted on 2010-08-31
3
1,037 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET MVC - Views 3 21
Run Stored Procedure uisng ADO 5 21
Can Angular be used with classic asp.net? 3 19
How to filter by key press ? 6 24
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

829 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