Solved

Format column on datatable

Posted on 2016-11-30
7
40 Views
Last Modified: 2016-11-30
Hello,
How can I  format this code on datasource

  dg(int).Columns("Total").DefaultCellStyle.Format = "N2"

Open in new window


dg is my datagridview and int is the number

instead on dg(int) how can I use datasource

Cheers
0
Comment
Question by:RIAS
[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
  • 2
  • 2
7 Comments
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41907073
Are you getting this from the database?

Do you want to do this in datatable? , You can do that in the query itself.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41907075
Basically - you can't. You can format strings and store these in the databable but numbers you do that when you display them.
0
 

Author Comment

by:RIAS
ID: 41907088
The problem is that it is a money field.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41907109
You format the value when you display it.  (Separating data and UI in designing the app)
0
 
LVL 29

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41907130
Use formatted from DB itself rather the in UI.

Try one of these approaches...

1. SELECT * , FORMAT(a,'0.00') FormattedColumn FROM CTE
2. SELECT * , CASE WHEN CHARINDEX('.',a,0) > 0 THEN SUBSTRING(CAST(a AS VARCHAR(10)),0,CHARINDEX('.',a,0)+2) ELSE a END FormattedColumn FROM CTE

FirstOne

;WITH CTE AS
(
	
	SELECT CAST(1.5 AS MONEY) a UNION ALL
	SELECT CAST(2.AS MONEY) UNION ALL
	SELECT CAST(3.2456AS MONEY) UNION ALL
	SELECT CAST(4.82AS MONEY) UNION ALL
	SELECT CAST(5AS MONEY) UNION ALL
	SELECT CAST(0.09 AS MONEY)
)
SELECT * , CASE WHEN CHARINDEX('.',a,0) > 0 THEN SUBSTRING(CAST(a AS VARCHAR(10)),0,CHARINDEX('.',a,0)+2) ELSE a END FormattedColumn FROM CTE

Open in new window

Second


;WITH CTE AS
(
	
	SELECT CAST(1.5 AS MONEY) a UNION ALL
	SELECT CAST(2.AS MONEY) UNION ALL
	SELECT CAST(3.2456AS MONEY) UNION ALL
	SELECT CAST(4.82AS MONEY) UNION ALL
	SELECT CAST(5AS MONEY) UNION ALL
	SELECT CAST(0.09 AS MONEY)
)
SELECT * , FORMAT(a,'0.00') FormattedColumn FROM CTE

Open in new window



Output


a                     FormattedColumn
--------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.50                  1.50
2.00                  2.00
3.2456                3.25
4.82                  4.82
5.00                  5.00
0.09                  0.09

(6 row(s) affected)

Open in new window


Hope it helps!!
0
 

Author Closing Comment

by:RIAS
ID: 41907133
Cheers Pawan!
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41907134
Cheers Rias !!

Just one update..

<<SELECT * , CASE WHEN CHARINDEX('.',a,0) > 0 THEN SUBSTRING(CAST(a AS VARCHAR(10)),0,CHARINDEX('.',a,0)+3) ELSE a END FormattedColumn FROM CTE>>
1

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

630 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