DataColumn Expression round results to 2 decimal places

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
Bob HoffmanDeveloperAsked:
Who is Participating?
RedKelvinConnect With a Mentor Commented:
Maybe you can format it using a mask

This example uses rounding, but in an unbound grid, on the InitializeRow event
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

Bob HoffmanDeveloperAuthor Commented:
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
Bob HoffmanDeveloperAuthor Commented:
.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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.