DataColumn Expression round results to 2 decimal places

Posted on 2007-10-10
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:HBHoffman
    LVL 22

    Expert Comment

    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

    LVL 8

    Author Comment

    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

    Maybe you can format it using a mask

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

    Author Comment

    .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

    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

    Join & Write a Comment

    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…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

    734 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

    24 Experts available now in Live!

    Get 1:1 Help Now