0

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
4

2 に答える 2

0

各ループの外側:

e.Row.Cells(index)

例えば。ヘッダーツールチップを追加する場合:

    Protected Sub Grid1_RowCreated(ByVal sender As Object, ByVal e As GridRowEventArgs)
       //setting row cells
    If e.Row.RowType = DataControlRowType.DataRow Then
      For i As Integer = 1 To e.Row.Cells.Count
        If i = 1 Then
           e.Row.Cells(i).Font.Size= 8
        ELSE
           e.Row.Cells(i).Font.Size= 12
        END IF
      NEXT
    End If
    End Sub
于 2012-05-31T07:24:01.093 に答える
0

C#でのKapilのコード

        private void DataGrid1_RowCreated(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 1; i < e.Row.Cells.Count; i++)
            {
                if (i == 1) { e.Row.Cells[i-1].Font.Bold = true; } else { e.Row.Cells[i-1].Font.Bold = false; }
            }
        }
    }
于 2015-01-23T20:28:24.343 に答える