?
Solved

How to format with $ in Datagrid and Decimal Type Column?

Posted on 2007-11-18
7
Medium Priority
?
1,580 Views
Last Modified: 2013-11-26
Hi,

I have a datatable that I am filling with a SQL Adapter. The sql query looks something like

SELECT price1,price2 from prices

Now, I do some calculations and want to display the values in the datatable with a $.
such as:

(See code snipped)

Here is the error:
Input string was not in a correct format.Couldn't store <$1,125.00> in sell Column.  Expected type is Decimal.

grr, how can I show the values with a $?
num3 = ((Convert.ToDecimal(mSubTable.Rows[i][5]) * (Convert.ToDecimal(mSubTable.Rows[i][6]) * .01M) + Convert.ToDecimal(mSubTable.Rows[i][5])));
mSubTable.Rows[i][7] = num3.ToString("$#,#,##.#0");

Open in new window

0
Comment
Question by:melegant
[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 22

Expert Comment

by:RedKelvin
ID: 20309401
Hi,
you will need to cast your column to string, this can be done in your SQL for example this is how you would achieve this in Oracle

SELECT to_char(price1), to_char(price2) from prices

then when the columns are bound to your grid they are considered to be strings instead of decimal columns, and you can add the $.

RedK
0
 
LVL 15

Expert Comment

by:Solar_Flare
ID: 20309407
you cant put a string in a decimal column, you have 2 options"

add another column with datatype string and put the values with $ symbols in that column, or if your datagrid has predefined columns then you can enter a format of "C" for that column and it will format it as a $ string for display.

if your datagrid needs in-place editing which needs to update the datasource then you will need to use the second method.
0
 

Author Comment

by:melegant
ID: 20309432
Solar,
My grid is being populated with a sqladapter and I am mapping the schema from the source. How can I format the column after the fact with the C as you mentioned?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:RedKelvin
ID: 20309511
Cast the columns to strings in the SQL as I already mentioned, What DB are you using?
0
 

Author Comment

by:melegant
ID: 20309552
Hi Red,

I can do that yes, however I would have to go and alter a bunch of SP's which I am not crazy about doing.

The issue here is that I have an Infragistics Ultragrid where I am displaying the values..and I want to display the values as currency. I am using SQL and my datatype for these particular fields is Decimal (18,0).

I know how to parse it once it is a currency (Numberstyles.Any with a Decimal.Parse).

There must be a way for the column to take a deicmal as currency..it would be crazy not to!

0
 
LVL 15

Accepted Solution

by:
Solar_Flare earned 1000 total points
ID: 20309652
Can you access the Columns collection of the grid? if you can then you might be able to set the format of the cells after  the columns have been generated...

in a normal datagridview control you have .Columns which is a collection, and then each column has a DefaultCellStyle and one of the style's properties is named Format which you can set.


maybe look at the ultragridview documentation and see if there are similar properties available, in which case you could do something like this


ultragridviewcolumn col = myGrid1.Columns["price1"];
col.DefaultCellStyle.Format = "C";

0
 

Author Comment

by:melegant
ID: 20309691
ha..that is it..and it was simple. Thanks! Have to do it during the grid init event...

e.Layout.Bands[0].Columns[6].Format = "$##,##.00";
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
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.…
Suggested Courses

762 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