altcolors.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace = "System.Data" %>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
' Since we're trying to keep things simple and so you can run this code
' easily, I'm just creating a table from scratch and not actually connecting
' to a DB to retrieve our sample data. I'm still using a DataGrid though so
' connecting up your own data source should be relatively straightforward.
' Just connect, get the data, and databind to the grid and you should be good.
Dim myTable As New DataTable
Dim myRow As DataRow
Dim iLetter As Integer
' Set up our table:
myTable.Columns.Add("ID", Type.GetType("System.Int32"))
myTable.Columns.Add("Title", Type.GetType("System.String"))
myTable.Columns.Add("Description", Type.GetType("System.String"))
' Fill it with sample data:
For iLetter = Asc("A") To Asc("M")
myRow = myTable.NewRow()
myRow("ID") = iLetter - 64
myRow("Title") = "The Letter " & Chr(iLetter)
myRow("Description") = "This is an upper case " & Chr(iLetter) & "."
myTable.Rows.Add(myRow)
Next iLetter
' Bind our table to the grid below.
AltColorsDataGrid.DataSource = myTable
AltColorsDataGrid.DataBind()
' Notice the formatting is handled completely by the grid itself.
' This is nice because this way when you want to change the look
' of the grid you don't need to worry about messing with your DB
' code. You can just change the attributes of the datagrid and
' you never have to get anywhere near anything else.
End Sub
</script>
<html>
<head>
<title>ASP.NET Alternating Colors Sample</title>
</head>
<body>
<form runat="server" EnableViewState="false">
<asp:DataGrid id="AltColorsDataGrid" runat="server"
Border = 0
CellSpacing = 0
CellPadding = 3
HeaderStyle-BackColor = "#CCCCCC"
HeaderStyle-ForeColor = "#000000"
HeaderStyle-Font-Bold = "True"
ItemStyle-BackColor = "#FFFFFF"
AlternatingItemStyle-BackColor = "#DDFFDD"
/>
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/altcolors_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>