以下代码是我的 gridview 的 rowdatabound 事件。它适用于除单元格文本格式作为货币之外的所有内容。事实上,我格式化货币的代码行会导致代码出错。如果我注释掉 FormatCurrency 行,则代码可以正常工作。为什么那行a)。不格式化单元格的文本和 b)。导致错误?
Protected Sub gvDataRetrieval_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvDataRetrieval.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dateindex As Integer = GetColumnIndexByHeaderText(gvDataRetrieval, "date")
e.Row.Cells(dateindex).Text = Split(e.Row.Cells(dateindex).Text, " ")(0)
For i As Integer = 0 To e.Row.Cells.Count - 1
e.Row.Cells(i).Font.Size = 10
e.Row.Cells(i).HorizontalAlign = HorizontalAlign.Center
If i > dateindex Then
If Convert.ToDecimal(e.Row.Cells(i).Text) < 0 Then
e.Row.Cells(i).ForeColor = Drawing.Color.Red
Else
e.Row.Cells(i).ForeColor = Drawing.Color.Black
End If
End If
e.Row.Cells(i).Text = FormatCurrency(e.Row.Cells(i).Text, 0)
Next
End If
End Sub