以下代碼是我的gridview的rowdatabound事件。它適用於除單元格文本格式之外的其他所有內容。實際上,我在格式化貨幣 的代碼行導致代碼出錯。如果我將FormatCurrency行註釋掉,代碼工作正常。爲什麼這一行a)。不格式化單元格的文本和b)。導致錯誤?在rowdatabound上格式化gridview單元格文本
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
什麼是錯誤? – codingbiz
輸入字符串的格式不正確,但似乎並非如此 – dinotom