根据列值更改字体颜色gridview行。不能索引gridviewrow?

时间:2011-02-01 19:52:16

标签: asp.net vb.net gridview sqldatasource

我在drr(5)遇到语法错误,其中5是我想基于颜色变化的列。 当我使用数据集

时,此方法有效
Dim Land As String = "Land"
        Dim Air As String = "Air"
        Dim Cruise As String = "Cruise"

        Dim y As String

        For Each drr As gridviewrow In GridView2.Rows
            y = drr(5).ToString()
            If y = Land Then
                e.Row.ForeColor = System.Drawing.Color.LightGreen
            ElseIf y = Air Then
                e.Row.ForeColor = System.Drawing.Color.Red
            ElseIf y = Cruise Then
                e.Row.ForeColor = System.Drawing.Color.Green
            End If


        Next

5 个答案:

答案 0 :(得分:1)

Dim a As String = GridView1.Rows(0).Cells(0).Text

使用Text变量,即单元格文字。

答案 1 :(得分:0)

如果要访问网格视图行中的单元格,则应使用该行的单元格属性。在您的示例中,您需要编写 drr.Cells(5).ToString() 如在

Dim Land As String = "Land"
    Dim Air As String = "Air"
    Dim Cruise As String = "Cruise"

    Dim y As String

    For Each drr As gridviewrow In GridView2.Rows
        y = drr.Cells(5).ToString()
        If y = Land Then
            e.Row.ForeColor = System.Drawing.Color.LightGreen
        ElseIf y = Air Then
            e.Row.ForeColor = System.Drawing.Color.Red
        ElseIf y = Cruise Then
            e.Row.ForeColor = System.Drawing.Color.Green
        End If


    Next

此外,我发现最好给行一个类,然后使用css更改颜色。

答案 2 :(得分:0)

您应该根据此值应用CssClass:

例如:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        BindGrid()
    End If
End Sub

Private Sub BindGrid()
    Dim dt As New DataTable
    dt.Columns.Add("ID", GetType(Int32)).AutoIncrement = True
    dt.Columns.Add("Style", GetType(String))
    dt.PrimaryKey = New DataColumn() {dt.Columns("ID")}
    Dim newRow As DataRow = dt.NewRow
    newRow("Style") = "Land"
    dt.Rows.Add(newRow)
    newRow = dt.NewRow
    newRow("Style") = "Air"
    dt.Rows.Add(newRow)
    newRow = dt.NewRow
    newRow("Style") = "Cruise"
    dt.Rows.Add(newRow)
    Me.GridView1.DataSource = dt
    Me.GridView1.DataBind()
End Sub    

Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim dr As DataRow = DirectCast(e.Row.DataItem, DataRowView).Row
        e.Row.CssClass = DirectCast(dr("Style"), String)
    End If
End Sub

重要的部分是在RowDataBound中,如果绑定GridView则会自动调用它。如果您不想将CssClass命名为与显示的文本完全相同,则可以使用If...ElseSelect Case来设置CSS类。

答案 3 :(得分:0)

关于改变TableCells?这也不行吗?

gridview.Rows[0].Cells[0].ForeColor = ColorTranslator.FromHtml("#0000FF");

在C#中,但试一试。

答案 4 :(得分:0)

Protected Sub grdUsers_RowDataBound(ByVal sender As Object, ByVal e As    System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdUsers.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
        For Each row As TableCell In e.Row.Cells
            If e.Row.Cells(11).Text = "Approved" Then
                For i As Integer = 0 To 11
                    e.Row.Cells(i).ForeColor = System.Drawing.Color.Green
                Next
            End If
            If e.Row.Cells(11).Text = "Rejected" Then
                For i As Integer = 0 To 11
                    e.Row.Cells(i).ForeColor = System.Drawing.Color.Red
                Next
            End If
        Next
    End If
End Sub