Esempio:
codice:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" Width="150px" />
------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As New DataTable
' Colonne
Dim c As New DataColumn("Colonna 1")
dt.Columns.Add(c)
Dim d As New DataColumn("Colonna 2")
dt.Columns.Add(d)
' Righe
Dim r1 As DataRow = dt.NewRow
r1.Item(0) = "dato1"
r1.Item(1) = "1"
dt.Rows.Add(r1)
Dim r2 As DataRow = dt.NewRow
r2.Item(0) = "dato2"
r2.Item(1) = "2"
dt.Rows.Add(r2)
Dim r3 As DataRow = dt.NewRow
r3.Item(0) = "dato3"
r3.Item(1) = "3"
dt.Rows.Add(r3)
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected 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
For ind As Integer = 0 To e.Row.Cells.Count - 1
Select Case e.Row.Cells(ind).Text.Trim
Case "1"
e.Row.Cells(ind).BackColor = Drawing.Color.Azure
Case "2"
e.Row.Cells(ind).BackColor = Drawing.Color.Blue
Case "3"
e.Row.Cells(ind).BackColor = Drawing.Color.Coral
End Select
Next
End If
End Sub
'Oppure se conosci l'indice della cella, cioe' se questo è fisso:
Protected 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
Select Case e.Row.Cells(1).Text.Trim
Case "1"
e.Row.Cells(1).BackColor = Drawing.Color.Azure
Case "2"
e.Row.Cells(1).BackColor = Drawing.Color.Blue
Case "3"
e.Row.Cells(1).BackColor = Drawing.Color.Coral
End Select
End If
End Sub