【问题标题】:ASP.NET - Use Column Name Instead of Index In GridViewRowEventArgs Row.Cells.ItemASP.NET - 在 GridViewRowEventArgs Row.Cells.Item 中使用列名而不是索引
【发布时间】:2011-08-08 20:02:41
【问题描述】:

我想对 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 实现这一点?

【问题讨论】:

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


    【解决方案1】:

    你可以这样做……但这是 C# 代码

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

    编辑:将此条件放在顶部

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

    【讨论】:

    • 我得到一个“被引用的对象的值为 'Nothing'。”当我尝试时。
    • 为什么我总是忘记标题栏?在您的帮助下,它现在可以工作了。我正在使用 "dr.Table.Columns("ON_TIME").Ordinal" 来获取索引号。
    猜你喜欢
    • 2018-03-08
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2021-09-09
    • 2018-04-07
    • 1970-01-01
    相关资源
    最近更新 更多