ASP.NET - 在GridViewRowEventArgs Row.Cells.Item中使用列名而不是索引

时间:2011-04-19 16:17:53

标签: .net asp.net vb.net gridview

我想对GridView中的单元格执行一些简单的自动格式化。到目前为止,我有以下代码:

Private Sub gridviewRefreshPanel_RowDataBound( _
    ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
        Handles gridviewRefreshPanel.RowDataBound

    Dim readyStatus As String = DataBinder.Eval(e.Row.DataItem, "READY")

    Select Case readyStatus
        Case "NO"
            e.Row.Cells.Item(5).ForeColor = Drawing.Color.Red
            e.Row.Cells.Item(5).Font.Bold = True
        Case "N/A"
            e.Row.Cells.Item(5).ForeColor = Drawing.Color.Goldenrod
            e.Row.Cells.Item(5).Font.Bold = True
        Case "YES"
            e.Row.Cells.Item(5).ForeColor = Drawing.Color.DarkGreen
            e.Row.Cells.Item(5).Font.Bold = True
    End Select

End Sub

我想通过列名而不是索引来引用单元格。例如,DataRow:

row.Item("ON_TIME")

如何使用GridView实现此目的?

1 个答案:

答案 0 :(得分:6)

你可以这样做......但这是c#code

DataRow dr = ((DataRowView)e.Row.DataItem).Row;
dr["ColumnName"]

编辑:将此条件置于顶部

if (e.Row.RowType == DataControlRowType.DataRow)