我有一个gridview,它有三列date,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事件我现在可以重置列中值的格式,但是基于单元格值的forecolor设置会导致错误(“输入字符串格式不正确”)。另外,我正在硬编码列索引。我需要一种方法来根据列标题名称
动态获取列索引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
答案 0 :(得分:0)
每个循环的外部:
e.Row.Cells(index)
EG。 用于添加标题工具提示:
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
答案 1 :(得分:0)
Kapil在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; }
}
}
}