Heitor Badotti
Heitor Badotti

Reputation: 188

DataGrid Format Cell property

My datagrid is not formatting the input value.

If I input "5", it returns "5" and not "5,00%"

I tried to change the cellstyle format of the cell in editor to "0,00%" or to "P", but it doesn't format... Already tried "000" or "0,00" and nothing happens... It always returns "5"

enter image description here enter image description here

Upvotes: 0

Views: 359

Answers (1)

Caius Jard
Caius Jard

Reputation: 74595

If it's any consolation, I couldn't reproduce your complaint:

enter image description here

I set everything up in code, but only because it makes it easier to paste onto SO. Paste this into a form and have a play around with it:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dt As New DataTable

    dt.Columns.Add("Thing", GetType(Double))
    dt.Rows.Add(1.234)
    dt.Rows.Add(2.345)

    Dim dg As New DataGridView
    dg.DataSource = dt

    Me.Controls.Add(dg) 'so that columns are autogenerated

    dg.Columns(0).DefaultCellStyle.Format = "0.00\%"

End Sub

You can do the same in the designer; doesn't have to be code

  • Add a New DataSet to your project, add a DataTable to it with a decimal capable column (pick better names than I have here):

enter image description here

  • Open the data sources panel (view menu, other windows), drag the datatable to the form to create a grid, edit the grid's columns to set the format:

enter image description here

  • and put some data in the underlying table:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.DataSet1.DataTable1.AddDataTable1Row(1.234) Me.DataSet1.DataTable1.AddDataTable1Row(2.345) End Sub

enter image description here

Upvotes: 1

Related Questions