[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2010-08-31
3
Medium Priority
?
1,068 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
[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
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 31

Accepted Solution

by:
MlandaT earned 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

656 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