?
Solved

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

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

770 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