0

我有一个gridview,它有三列日期,pnl,cumpnl 我可以在使用代码应用于所有单元格时添加一些格式,例如

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

卡皮尔的 C# 代码

        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 回答
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 回答