How to color code rows conditionally in VB and SQL
I am developing an ASPX file to return all records from a SQL table. I can successfully get all of the numbers to show, but now I want certain rows to have their background colors changed if they meet certain criteria. So I want to compare 2 of my columns to some value, and if it exceeds this value then it sho开发者_如何学JAVAuld change color of that row. How can I fix below code? Main problem is I don't know how to specify a column of data to compare. No errors on this, but none of my rows have color changes either.
ASPX excerpt:
Sub PrintMessageGrid_RowDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim Six_In_A_Row As Integer = _
Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Six_In_A_Row"))
If Six_In_A_Row = "1" Then
' color the background of the row yellow
e.Row.BackColor = Drawing.Color.Yellow
End If
End If
End Sub
And my HTML:
<ASP:GridView id="dgTable" runat="server" AUTOGENERATECOLUMNS="true" ShowHeader="true" OnItemDataBound="PrintMessageGrid_RowDataBound">
<HEADERSTYLE BackColor = "#336699" ForeColor = "#ffffff" Font-Bold = "true" />
</ASP:GridView>
I think the problem is how you access the value in the cells... maybe you can access cell value by using the GridViewRowEventArgs like this :
e.Row.Cells(1).Text
Maybe your code works too... you have only to change the condition! You are comparing an integer with a string... so your code must be changed in this code :
If Six_In_A_Row = 1 Then .....
.Tostring() is missing in DataBinder could be an issue.
And
In If Six_In_A_Row = "1" Then
, you are comparing int with string. Do Six_In_A_Row.ToString() = "1" then
Use this example : http://www.dotnetwatch.com/change-the-specific-Row--Col389_AR.aspx
e.Row.BackColor = System.Drawing.Color.FromArgb(255,234,255); // Full row color
e.Row.Cells[3].BackColor = System.Drawing.Color.Yellow; // Column color
精彩评论