在System.Data.DataTable中显示货币

时间:2014-09-15 21:31:48

标签: vb.net system.data.datatable

我正在处理一些旧的VB代码,它创建了一个System.Data.DataTable并定义了一些列。它工作正常,除了我需要某个列显示为货币,而不仅仅是一个浮点数。我怎么做?

Dim myDataTable As New System.Data.DataTable("tblRec")
myDataTable.Columns.Add("RECAMOUNT", System.Type.GetType("System.Double"))

Protected WithEvents DataGridCurrentCRLines As Global.System.Web.UI.WebControls.DataGrid
Session("CRLines") = myDataTable
DataGridCurrentCRLines.DataSource = Session("CRLines")

将行更改为:

myDataTable.Columns.Add("RECAMOUNT", System.Type.GetType("System.Decimal"))

没有区别,我的意思是显示1234567.89,而不是1,234,567.89

1 个答案:

答案 0 :(得分:0)

@Time Schmelter的提示指向正确的方向。
首先将类型更改为字符串:

myDataTable.Columns.Add("RECAMOUNT", System.Type.GetType("System.String")

然后我编写了一个帮助方法将String转换为货币String,看来一个人不能直接从String转换为货币String,而是你必须要String - >十进制 - >货币字符串

 Private Function ConvertStringToCurrencyString(ByVal inputString As String) As String

        Dim currencyString As String
        currencyString = ""
        Dim myDecimal As Decimal

        Try

            myDecimal = System.Convert.ToDecimal(inputString)
            currencyString = myDecimal.ToString("C2")

        Catch exception As System.OverflowException
            System.Console.WriteLine("ConvertStringToCurrencyString(): Overflow in string-to-decimal conversion.")
        Catch exception As System.FormatException
            System.Console.WriteLine("ConvertStringToCurrencyString(): The string is not formatted as a decimal.")
        Catch exception As System.ArgumentException
            System.Console.WriteLine("ConvertStringToCurrencyString(): The string is null.")
        End Try

        Return currencyString

    End Function