ASP.NET - use column name instead of index in GridViewRowEventArgs Row.Cells.Item

I want to do some simple auto formatting for cells in my GridView. So far I have the following code:

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

I would like to refer to cells by column name, not by index. For example, DataRow:

row.Item("ON_TIME")

How to achieve this using GridView?

+5
source share
1 answer

you can do like .. but this is C # code

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

Edit: Put this condition at the beginning

if (e.Row.RowType == DataControlRowType.DataRow)
+6
source

All Articles