Decimals and thousands separators in a numeric DataGrid Column

Posted on 2004-11-07
Last Modified: 2009-12-16

I’d like column1 of my datagrid to be formatted to display numbers with a fixed number of decimals (e.g. 1, 2 or 3) and yet be formatted with  thousands  separators.

Does anyone know how to do that?

To format with thousands separators this is the code I use but it doesn’t allow for decimals:

Dim dgTextBoxColumn1 As DataGridTextBoxColumn = MyDataGrid.TableStyles("tProduits").GridColumnStyles("MyColumn")
dgTextBoxColumn1.Format = "#,#"

Any help shall be greatly appreciated.
Question by:Ghanisen
    LVL 28

    Accepted Solution

    you need to create TableStyle

    Public Sub AddFormattedColumn()
        Dim tsProducts As New DataGridTableStyle
        tsProducts.MappingName = "Products"

        ' Create a new column style
        Dim csUnitPrice As New DataGridTextBoxColumn()
        csUnitPrice.MappingName = "UnitPrice"
        csUnitPrice.HeaderText= "Unit Price"
        ' Set the format of the column
        ' NOTE: The data must be of type Integer to for it to be
        ' formatted correctly as a currency.
        csUnitPrice.Format = "c"

    End Sub

    Format   Input                                                   Output
    d           February 12, 1976                                 2/12/1976
    D           February 12, 1976                                 Thursday, February 12, 1976
    T           11:38:00 PM                                         11:38:00 PM
    t            11:38:00 PM                                         11:38 PM
    F           Thursday, February 12, 1976 11:38:16 PM Thursday, February 12, 1976 11:38:16 PM
    f            Thursday, February 12, 1976 11:38:16 PM Thursday February 12, 1976 11:38
    C           32.98                                                   $32.98


     Dim col1 As DataGridTextBoxColumn
       col1 = New DataGridTextBoxColumn()
       col1.TextBox.Enabled = False
       col1.Format = "[#0]" 'write number in [..]
       col1.Alignment = HorizontalAlignment.Right
       col1.HeaderText = "ID"
       col1.MappingName = "employeeid"
       Dim col2 As DataGridTextBoxColumn
       col2 = New DataGridTextBoxColumn()
       col2.HeaderText = "Last Name"
       col2.MappingName = "lastname"

    Author Comment

    Hi Iboutchkine,

    My question wasn't about currency data but only plain numeric data. So it should be "n" instead of "c".

    I found also another way to do it with a custom format:

     Dim dgTextBoxColumn6 As DataGridTextBoxColumn = _
                   dgr.TableStyles("tProduits"). _
            ' Format avec séparateur de milliers prédéfini
            dgTextBoxColumn6.Format = "#.#0,000"

    The number of 0 after the coma determines the number of decimals. It works.

    Thank you for your effort. You still deserve 500 points. Bye Iboutchkine.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    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…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    729 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

    20 Experts available now in Live!

    Get 1:1 Help Now