2017-08-24 60 views
0

我想突出顯示基於我的數據集(圖片附加)11列數量的行。我預計在第11列< 5000中有價值的所有行都將以紅色突出顯示。下面是我的代碼:RowDataBound行高亮

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記錄,但沒有標註上顯示。請指教。

[my data set][1] 


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

回答

0

這是你正在努力實現的最小工作示例。 在下面的代碼片段中,列2小於100的行獲得不同的顏色。

WebForm1.aspx的

<%@ 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

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 ...

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

布拉 - 爲您輸入的感謝。然而,我想讓我的代碼工作,因爲我知道數據(行)被收集到我的alist gvRsrvtionValdtn_RowDataBound中。問題是如何讓它們顯示。你有什麼主意嗎? – VBlearning

+0

添加了修改的方法。如果您希望我們使用您的實際代碼,則需要提供實際的代碼......如果您粘貼了您在全新項目中提供的代碼,則會看到我們缺少的內容。你的代碼片段不會告訴我們什麼是'alist'和'ds'。 –

+0

474199 | \t PEDERSL | 4/12/2013 13:19 | \t 2013年6月1日0:00 \t | 189 | \t QC-7/6013電源板| |銅黃銅銷售| \t -1 | \t未定義的報告流程路徑| \t 15000 | \t 41300 | \t是\t |是 – VBlearning

0

我忘了進入行級別。加入這一行後:對於每個小區的TableCell在myRow.Cells內如果受保護的子gvRsrvtionValdtn_RowDataBound的聲明,它的工作原理:

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