简体   繁体   English

datagridview不显示货币格式

[英]datagridview not displaying currency format

Firstly I used the following to populate the DataGridView : 首先,我使用以下内容填充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: 然后,我格式化了包含以下代码的DataGridView:

    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. 列将其值显示为$ 1234.00。 When new values were added to the columns they immediately displayed as $1234.00. 将新值添加到列中后,它们立即显示为$ 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. 如果在创建数据集时某列没有任何值,则该数据列的datagridview中不会显示任何值。 (no problem so far) (到目前为止没有问题)

However, all new values added to the blank column display as 1234.00. 但是,所有添加到空白列的新值都显示为1234.00。 Not $1234.00. 不是$ 1234.00。

I have tried refreshing the DataGridView I have re-formatted the DataGridView after the change to the cell. 我尝试刷新DataGridView,在更改单元格后重新格式化了DataGridView。 It still displays as 1234.00. 它仍然显示为1234.00。

If I save the changes, recreate the DataSet and repopulate the DataGridView all is OK. 如果我保存更改,请重新创建DataSet并重新填充DataGridView,一切正常。

I need the DataGridView to reflect the correct format ($1234.00) when new values are added directly to the column????? 当新值直接添加到列中时,我需要DataGridView反映正确的格式($ 1234.00)。

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. 它必须可以工作,但是如果您的数据库列格式不是十进制 (而是TEXT),则这种格式将永远无法使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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