DataColumn Expression round results to 2 decimal places

Posted on 2007-10-10
Medium Priority
Last Modified: 2013-11-07
I have a DataColumn expression that yields a result with 10 decimal places, I need to round this to 2 decimal places. I've tried wrapping my expression with the Convert(expression, type) function, I've also tried DataColumn.Format("#.00") but nothing gets me to 2 decimal places. The DataColumn with the expression is of data type decimal(15,2).... Can anyone offer any suggestions?  Thanks
Question by:Bob Hoffman
  • 2
  • 2
LVL 22

Expert Comment

ID: 20053869
try manipulating the datasource before you bind it to the grid

otherwise you may need to round the values inside the itemdatabound event for the grid

  Protected Sub gridname_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles gridname.ItemDataBound

      e.Item.Cells(0).Text = Math.Round(CDbl(e.Item.Cells(0).Text), 2)

End Sub

the 0 in the cells array refers to column 0


Author Comment

by:Bob Hoffman
ID: 20054740
I'm using Infragistics UltraWinGrid grid, it has an event similar to ItemDataBound. I haven't figured out how to change the cell text, seems to be ready only. Thanks
LVL 22

Accepted Solution

RedKelvin earned 1500 total points
ID: 20055612
Maybe you can format it using a mask

This example uses rounding, but in an unbound grid, on the InitializeRow event

Author Comment

by:Bob Hoffman
ID: 20059123
.Format = "c" gave me the decimal percision I wanted but I need the percent sign, .Format = "p" did not work because it re-calculated the value as a percentage. Turns out if you have the percent sign in your format string your value will be re-calculated as a percentage. If you want the value left alone but format and round it you need to put the percent sign in single quotes... .Format = "#.00'%'"

Thanks, your suggestions pointed me on the right direction.

Featured Post

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.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

864 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