首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RowDataBound行突出显示

RowDataBound行突出显示
EN

Stack Overflow用户
提问于 2017-08-25 00:48:50
回答 2查看 146关注 0票数 0

我正在尝试根据我的数据集(附图)上第11列的数字来突出显示行。我希望值在第11列< 5000的所有行都将突出显示为红色。下面是我的代码:

代码语言:javascript
复制
  Protected Sub loadData()    
        gvRsrvtionValdtn.DataSource = ds
                Dim myTable As System.Data.DataRowCollection
                myTable = ds.Tables(0).Rows
                If myTable.Count > 0 Then
                    For i = 0 To myTable.Count - 1
                        If myTable(i)(10) > 5000 Then
                            alist.Add(i)
                        End If    
                Next
            End If

            gvRsrvtionValdtn.DataBind()
            btnExp.Visible = True
        End Sub
    Protected Sub gvRsrvtionValdtn_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvRsrvtionValdtn.RowDataBound
            Dim myRow As TableRow = e.Row()
            If alist.Contains(e.Row.RowIndex) Then
               myRow.BackColor = Color.Red
            End If
        End Sub



 <asp:GridView ID="gvRsrvtionValdtn" runat="server" AutoGenerateColumns="False" 
                BackColor="Black" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
                CellPadding="4" CssClass="aspdatagrid" ForeColor="Black" CellSpacing="1"
                HeaderStyle-CssClass="fixHdr" Width="98%" EmptyDataText="No records found" 
                EmptyDataRowStyle-CssClass="emptyData" RowStyle-Wrap="false"
                **OnRowDataBound ="gvRsrvtionValdtn_RowDataBound"**> 

在调试时,我可以看到第11列的值< 5000的记录进入alist,但在显示时没有突出显示。请给我建议。

代码语言:javascript
复制
[my data set][1]


  [1]: https://i.stack.imgur.com/gm7n8.jpg
EN

回答 2

Stack Overflow用户

发布于 2017-08-25 05:43:38

以下是您正在尝试实现的最小工作示例。在下面的代码片段中,列2小于100的行将获得不同的颜色。

WebForm1.aspx

代码语言:javascript
复制
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="HighlightRowsInDGV_Web.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>below this<br />
        <asp:GridView ID="gvRsrvtionValdtn" runat="server" AutoGenerateColumns="true" 
                BackColor="green" BorderColor="#DEDFDE" BorderStyle="Double" BorderWidth="1px" 
                CellPadding="4" CssClass="aspdatagrid" ForeColor="Black" CellSpacing="1"
                HeaderStyle-CssClass="fixHdr" Width="98%" EmptyDataText="No records found" 
                EmptyDataRowStyle-CssClass="emptyData" RowStyle-Wrap="false"
                OnRowDataBound ="gvRsrvtionValdtn_RowDataBound"></asp:GridView>

    </div>
    </form>
</body>
</html>

WebForm1.aspx.vb

代码语言:javascript
复制
Imports System.ComponentModel

Public Class WebForm1
    Inherits System.Web.UI.Page
    Public alist As New BindingList(Of entry)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim oneentry As New entry()
        oneentry.blah = "blah blah blah 1"
        oneentry.bleh = 50

        Dim anotherEntry As New entry()
        anotherEntry.blah = "blah blah blah 2"
        anotherEntry.bleh = 100

        Dim yetagain As New entry()
        yetagain.blah = "blah blah blah 3"
        yetagain.bleh = 25

        alist.Add(oneentry)
        alist.Add(anotherEntry)
        alist.Add(yetagain)
        loadData()
    End Sub


    Protected Sub loadData()
        gvRsrvtionValdtn.DataSource = alist
        gvRsrvtionValdtn.DataBind()
    End Sub
    Protected Sub gvRsrvtionValdtn_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvRsrvtionValdtn.RowDataBound
        Dim ref As Integer = 100
        If e.Row.RowIndex > -1 Then 'if this is not the header row
            If CType(e.Row.Cells(1).Text, Integer) < 100 Then
                e.Row.BackColor = Drawing.Color.AliceBlue
            End If
        End If
    End Sub

    Public Class entry
        Public Property blah As String
        Public Property bleh As Integer
    End Class

End Class

在你的评论之后,这里有一个更新的方法。你的问题和问题有点不清楚。你的开场问题是...

如果列中的数据小于5000,则突出显示该行...

这就是代码片段所做的,尽管我的代码是在100而不是5000上执行的……

代码语言:javascript
复制
Protected Sub gvRsrvtionValdtn_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvRsrvtionValdtn.RowDataBound
    Dim ref As Integer = 100
    If e.Row.RowIndex > -1 Then 'if this is not the header row
        'If CType(e.Row.Cells(1).Text, Integer) < 100 Then
        '    e.Row.BackColor = Drawing.Color.AliceBlue
        'End If
        If alist.Contains(e.Row.DataItem) And CType(e.Row.DataItem, entry).bleh < 100 Then
            e.Row.BackColor = Drawing.Color.AliceBlue
        End If
    End If

End Sub
票数 0
EN

Stack Overflow用户

发布于 2017-08-25 23:32:44

我忘了访问行级。添加下面这一行之后:对于每个在受保护的子gvRsrvtionValdtn_RowDataBound的IF语句中作为myRow.Cells中的TableCell的单元格,它都是有效的:

代码语言:javascript
复制
Protected Sub gvRsrvtionValdtn_RowDataBound(sender As Object, e As 
GridViewRowEventArgs)
    Dim myRow As TableRow = e.Row()
    If e.Row.RowIndex > -1 Then
        If alist.Contains(e.Row.RowIndex) Then
            For Each cell As TableCell In myRow.Cells
                cell.BackColor = Color.Red
            Next
        End If
    End If
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45867058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档