Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Decimals and thousands separators in a numeric DataGrid Column

Posted on 2004-11-07
Medium Priority
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

iboutchkine earned 1500 total points
ID: 12517742
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

ID: 12518976
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

564 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