根据列值更改网格视图行的字体颜色。无法索引gridviewrow吗?

发布于 2024-10-15 23:28:40 字数 616 浏览 6 评论 0原文

我在 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦萦几度 2024-10-22 23:28:40
Dim a As String = GridView1.Rows(0).Cells(0).Text

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

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

use Text variable , that is the cell text word.

别再吹冷风 2024-10-22 23:28:40

当您想要访问网格视图行中的单元格时,应该使用该行的单元格属性。在您的示例中,您需要编写 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 更改颜色。

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.

薄荷→糖丶微凉 2024-10-22 23:28:40

您应该根据这个值应用一个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 类。

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.

柠檬色的秋千 2024-10-22 23:28:40

关于更改 TableCells?这也行不通?

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

在 C# 中,但尝试一下。

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.

飞烟轻若梦 2024-10-22 23:28:40
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文