date、pnl、cumpnlの3列のグリッドビューがあります。次のようなコードを使用してすべてのセルに適用すると、書式を追加できます。
Protected Sub OnRowCreated(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
For Each cell As TableCell In e.Row.Cells
cell.Font.Size = 10
cell.HorizontalAlign = HorizontalAlign.Center
Next
End If
End Sub
そのFor...Nextループ内で、pnl列とcumpnl列のセルのみを参照するにはどうすればよいですか?とにかく、列ヘッダー名またはインデックスでセルを参照することはできません。
アップデート:
RowDataBoundイベントを使用することで、列の値の形式をリセットできるようになりましたが、セルの値に基づくフォアカラーの設定が誤ったままになります(「入力文字列が正しい形式ではありません」)。また、列インデックスをハードコーディングしています。列ヘッダー名に基づいて列インデックスを動的に取得する方法が必要です
Protected Sub gvDataRetrieval_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDataRetrieval.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 1 To 2
e.Row.Cells(i).Text = FormatCurrency(e.Row.Cells(i).Text, 2).ToString()
If Double.Parse(e.Row.Cells(i).Text) < 0 Then e.Row.Cells(i).ForeColor = Drawing.Color.Red
Next
End If
End Sub