我正在编写一些旧的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,而不是1234567.89
@Time-Schmelter的提示指向正确的方向
首先将类型更改为字符串:
myDataTable.Columns.Add("RECAMOUNT", System.Type.GetType("System.String")
然后我写了一个助手方法来将String转换为货币String,似乎不能直接从String转换为币种String,而是必须转换为String->Decimal->CurrencyString
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