<%@ Page Language="VB" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Data" %>
<script runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
BindGrid()
End Sub
Sub BindGrid()
Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ColtAtAspAlliance"))
Dim myDataAdpater As New SqlDataAdapter("Select Top 10 * from Colt_Products", myConnection)
Dim myDataSet As DataSet
Try
myDataSet = New DataSet()
myDataAdpater.Fill(myDataSet)
DataGrid1.DataSource = myDataSet
DataGrid1.DataBind()
DataGrid2.DataSource = myDataSet
DataGrid2.DataBind()
Catch SQLEx As SqlException : Response.Write(SQLEx.Message.ToString())
Catch Ex As Exception : Response.Write(Ex.Message.ToString())
End Try
End Sub
Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor=
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=
End If
End Sub
Sub DataGrid2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Cells(1).Attributes.Add("onmouseover", "this.style.backgroundColor=
e.Item.Cells(1).Attributes.Add("onmouseout", "this.style.backgroundColor=
e.Item.Cells(3).Style("cursor") = "hand"
e.Item.Cells(3).Attributes.Add("onclick", "alert('You click at ID: " & e.Item.Cells(0).Text & "!');")
End If
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<table>
<tbody>
<tr>
<td>
<b>1. Color change per Row:</b>
<br />
<br />
<asp:DataGrid id="DataGrid1" runat="server" OnItemDataBound="DataGrid1_ItemDataBound" CellPadding="4" BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#CC9966" EnableViewState="False" AutoGenerateColumns="False">
<SelectedItemStyle font-bold="True" forecolor="#663399" backcolor="#FFCC66"></SelectedItemStyle>
<ItemStyle forecolor="#330099" backcolor="White"></ItemStyle>
<HeaderStyle font-bold="True" forecolor="#FFFFCC" backcolor="#990000"></HeaderStyle>
<FooterStyle forecolor="#330099" backcolor="#FFFFCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="ProductID" HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="ProductName" HeaderText="Product Name"></asp:BoundColumn>
<asp:BoundColumn DataField="QuantityPerUnit" HeaderText="Quantity Per Unit"></asp:BoundColumn>
<asp:BoundColumn DataField="ReorderLevel" HeaderText="Reorder Level"></asp:BoundColumn>
</Columns>
<PagerStyle horizontalalign="Center" forecolor="#330099" backcolor="#FFFFCC"></PagerStyle>
</asp:DataGrid>
</td>
</tr>
<tr>
<td>
<p>
<hr />
</p>
<p>
<br />
<b>2. Features: </b>
</p>
<p>
<b>- Color changed per "column" - (2nd Column: "ProductName") </b>
</p>
<p>
<b>- "Little Hand Cursor & MessageBox onClick" - (4th Column: Reorder
Level):</b>
<br />
<br />
<asp:DataGrid id="DataGrid2" runat="server" OnItemDataBound="DataGrid2_ItemDataBound" CellPadding="4" BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#CC9966" EnableViewState="False" AutoGenerateColumns="False">
<SelectedItemStyle font-bold="True" forecolor="#663399" backcolor="#FFCC66"></SelectedItemStyle>
<ItemStyle forecolor="#330099" backcolor="White"></ItemStyle>
<HeaderStyle font-bold="True" forecolor="#FFFFCC" backcolor="#990000"></HeaderStyle>
<FooterStyle forecolor="#330099" backcolor="#FFFFCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="ProductID" HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="ProductName" HeaderText="Product Name"></asp:BoundColumn>
<asp:BoundColumn DataField="QuantityPerUnit" HeaderText="Quantity Per Unit"></asp:BoundColumn>
<asp:BoundColumn DataField="ReorderLevel" HeaderText="Reorder Level"></asp:BoundColumn>
</Columns>
<PagerStyle horizontalalign="Center" forecolor="#330099" backcolor="#FFFFCC"></PagerStyle>
</asp:DataGrid>
</p>
</td>
</tr>
</tbody>
</table>
<!-- Insert content here -->
</form>
</body>
</html>