Solved

Format column on datatable

Posted on 2016-11-30
7
17 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 17

Expert Comment

by:Pawan Kumar Khowal
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 17

Accepted Solution

by:
Pawan Kumar Khowal 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 17

Expert Comment

by:Pawan Kumar Khowal
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

21 Experts available now in Live!

Get 1:1 Help Now