简体   繁体   中英

datagridview not displaying currency format

Firstly I used the following to populate the DataGridView :

        dta = New OleDbDataAdapter("Select * From [" & ActName & "$B6:E" & LastEntryRow & "]", cn)
        dts = New DataSet
        dta.Fill(dts, "Detailtable")
        DataGridView1.DataSource = dts
        DataGridView1.DataMember = "Detailtable"

I then formatted the DataGridView which included the following code:

    Dim currencyCellStyle As New DataGridViewCellStyle
    currencyCellStyle.Format = "C2"

    With Me.DataGridView
        .Columns(1).DefaultCellStyle = currencyCellStyle
        .Columns(2).DefaultCellStyle = currencyCellStyle
    End with

This worked well. Columns displayed their values as $1234.00. When new values were added to the columns they immediately displayed as $1234.00. (working so far)

If a column did not have any values when the dataset was made, no values showed in the datagridview for that column. (no problem so far)

However, all new values added to the blank column display as 1234.00. Not $1234.00.

I have tried refreshing the DataGridView I have re-formatted the DataGridView after the change to the cell. It still displays as 1234.00.

If I save the changes, recreate the DataSet and repopulate the DataGridView all is OK.

I need the DataGridView to reflect the correct format ($1234.00) when new values are added directly to the column?????

If you use this line

DataGridView1.Columns(1).DefaultCellStyle.Format = "C2"

it must be work but if your database column format is not Decimal (but TEXT), this type of format it'll never works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM