How to Format DataGridView Cell

Posted on 2009-02-19
Last Modified: 2013-12-17
Good Day:

I am creating a WinForms application using VS.NET 2005, VB.NET, and the DataGridView control.  The Unit Price cell on my DataGridView control rounds my number and I do not want the numbers to be rounded.  For example, .375 gets rounded to .38.  How do I format so it does not round the number?  My format code is as follows:

grdDetail.Columns("Unit Price").DefaultCellStyle.Format = "#,###,###.00"

Any Suggestions?
Thank You,
Question by:DeniseGoodheart
    LVL 8

    Accepted Solution

    You can try creating a custom formatter/format provider class by implementing the IFormaterProvider and ICustomFormatter interfaces.

    Using the the custom format provider below you can round down a number by setting the "Format" property to "{0:v}" and setting the "FormatProvider" property to "New RoundDownFormatter()".

    You can test the below class by running the following:

    double number = 0.3366;
                MessageBox.Show(string.Format(new RoundDownFormatter(), "{0:v}", number));

        Public Class RoundDownFormatter
            Implements IFormatProvider, ICustomFormatter
            Public Function GetFormat(ByVal formatType As System.Type) As Object Implements System.IFormatProvider.GetFormat
                If (formatType Is GetType(ICustomFormatter)) Then
                    Return Me
                End If
                Return Nothing
            End Function
            Public Function Format(ByVal format1 As String, ByVal arg As Object, ByVal formatProvider As System.IFormatProvider) As String Implements System.ICustomFormatter.Format
                If (format1 <> String.Empty And arg.GetType() Is GetType(Double)) Then
                    If (format1.StartsWith("v")) Then
                        Dim result As Double = Math.Floor(CType(arg, Double) * 100) / 100
                        Return result.ToString()
                    End If
                End If
                Return Nothing
            End Function
        End Class

    Open in new window


    Author Comment


    Thank you for posting your code. I was hoping for a simple format code.  There has to be an easier way.

    Author Comment


    I figured it out.  I simply added an extra 0 at the end as follows:
    Me.grdDetail.Columns("Unit Price").DefaultCellStyle.Format = "#,###,###.000"

    LVL 8

    Expert Comment

    That's no prob. Apologies, i assumed you wanted the value to two decimal places without rounding up.
    For any reference to existing standard or custom numeric format specifiers. Just check out:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    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.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 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

    8 Experts available now in Live!

    Get 1:1 Help Now