Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET - Use Column Name Instead of Index In GridViewRowEventArgs Row.Cells.Item

I want to perform some simple auto-formatting to the 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'd like to refer to the cells by the column name rather than the index. For example, DataRow:

row.Item("ON_TIME")

How do I achieve this with a GridView?

like image 596
tgxiii Avatar asked Apr 19 '11 16:04

tgxiii


1 Answers

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

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

Edit: Put this condition at the top

if (e.Row.RowType == DataControlRowType.DataRow)
like image 56
Muhammad Akhtar Avatar answered Oct 03 '22 08:10

Muhammad Akhtar