change the font color gridview row based on a columns value. cant index gridviewrow?

笑着哭i 提交于 2020-01-06 05:37:05

问题


i am getting a syntax error at drr(5) which 5 is the column i want to base the color change on. this method works when i am using a dataset

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

回答1:


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

use Text variable , that is the cell text word.




回答2:


When you want to access a cell in a grid view row, you should use the cell property of the row. In your example you need to write drr.Cells(5).ToString() As in

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

Also I find that it's better to give the row a class and then change the color using css.




回答3:


You should apply a CssClass according to this value:

for example:

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

The important part is in RowDataBound that is called automatically if you bind the GridView. If you don't want to name the CssClass exactly like the text that is displayed, you could use If...Else or Select Case to set the CSS-Class.




回答4:


And about changing the TableCells? This doen't work too?

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

In C#, but give a try.




回答5:


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


来源:https://stackoverflow.com/questions/4867283/change-the-font-color-gridview-row-based-on-a-columns-value-cant-index-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!