0

我有一个 datagridview,它从数据库加载数据。SPENDING_SUM 在数据库中是双倍的,但我想在 datagridview 中将其更改为金钱(例如,如果数据库值为 1234567.00,我想在 datagridview 中显示的是 $1,234,567.00)。不知道如何转换价值。

相关代码为:

       Using connObj As New SqlClient.SqlConnection(My.Settings.dbConnectionString)
            connObj.Open()


            querySql = "SELECT UPPER(USER_ID) AS 'USER ID', UPPER(USER_NAME) AS 'USER NAME', " & vbCrLf &
                      "CONVERT(varchar,JOIN_DATE, 103) AS 'JOIN DATE'," & vbCrLf &
                      "SPENDING_SUM AS 'SPENDING SUM' FROM USERS WHERE USER_NAME = " & vbCrLf &


            querySql = querySql + userName.Text

            Try
                adapter = New SqlDataAdapter(querySql, connObj)
                adapter.Fill(ds)
                DataGridView1.DataSource = ds.Tables(0)
            Catch ex As Exception
                logger.Error(ex.ToString)
            End Try

            connObj.Close()
        End Using
4

2 回答 2

0

假设您有一个名为 lblDisplay 的控件,您想在其中显示它,这里是简单的代码。

 lblDisplay.Text = String.Format("{0:C2}", SPENDING_SUM)

为了简单的使用,试试这个例子。

 Dim Numbers As Double = 1234567.0
 lblDisplay.Text = String.Format("{0:C2}", Numbers)

希望这回答了你的问题。

于 2015-01-30T04:55:28.093 回答
0

要显示带有前面的美元($)符号的归档值,您无需将该字段转换为货币,而是可以使用类似的东西

 querySql = "SELECT UPPER(USER_ID) AS 'USER ID', UPPER(USER_NAME) AS 'USER NAME', " & vbCrLf &
                      "CONVERT(varchar,JOIN_DATE, 103) AS 'JOIN DATE'," & vbCrLf & 
                      " '$' + convert(varchar(100),SPENDING_SUM) AS 'SPENDING SUM' FROM USERS WHERE USER_NAME = " & vbCrLf &

如果你想转换成钱,你可以使用像

 querySql = "SELECT UPPER(USER_ID) AS 'USER ID', UPPER(USER_NAME) AS 'USER NAME', " & vbCrLf &
                      "CONVERT(varchar,JOIN_DATE, 103) AS 'JOIN DATE'," & vbCrLf &
                      "cast(SPENDING_SUM as money) AS 'SPENDING SUM' FROM USERS WHERE USER_NAME = " & vbCrLf &

无论如何,我更喜欢第一个解决方案,因为它将提供更好的查询性能。

于 2015-01-30T04:38:20.080 回答