Solved

Format column on datatable

Posted on 2016-11-30
7
29 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 25

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 25

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 25

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

19 Experts available now in Live!

Get 1:1 Help Now