<%@ Page Language="VB" %> <%@ import Namespace="System.Data.OleDb" %> <%@ import Namespace="System.Data" %> <script runat="server"> Sub Page_Load(Sender As Object, E As EventArgs) BindGrid() End Sub
Sub BindGrid() Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source =" + Server.MapPath(".")+"/datagrid.mdb" Dim myConnection As OleDbConnection myConnection = New OleDbConnection(connstr) Dim myDataAdpater As New OleDbDataAdapter("Select Top 10 * from data", myConnection) Dim myDataSet As DataSet Try myDataSet = New DataSet() '---------------------------------- ' Fill the DataSet for the DataBinding of the 2 DataGrid '---------------------------------- myDataAdpater.Fill(myDataSet) DataGrid2.DataSource = myDataSet DataGrid2.DataBind() Catch SQLEx As OleDbException : Response.Write(SQLEx.Message.ToString()) Catch Ex As Exception : Response.Write(Ex.Message.ToString()) End Try 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 '--------------------------------------------------- ' Add the OnMouseOver and OnMouseOut method a Cell (Column) of DataGrid '--------------------------------------------------- e.Item.Cells(1).Attributes.Add("onmouseover", "this.style.backgroundColor='#DDEEFF'") e.Item.Cells(1).Attributes.Add("onmouseout", "this.style.backgroundColor='white'") e.Item.Cells(2).Attributes.Add("onmouseover", "this.style.backgroundColor='#CC3300'") e.Item.Cells(2).Attributes.Add("onmouseout", "this.style.backgroundColor='white'") e.Item.Cells(2).Style("cursor") = "hand" '--------------------------------------------------- ' Change the Mouse Cursor of a particular Cell (Column) of DataGrid ' (Or you may do it for a whole Row of DataGrid :) '--------------------------------------------------- e.Item.Cells(3).Style("cursor") = "hand" '--------------------------------------------------- ' Add the OnClick Alert MessageBox to a particular Cell (Column) of DataGrid '--------------------------------------------------- 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"> <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> </form>
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
<tr onmouseover="this.style.backgroundColor='#e6f1d8'" onmouseout="this.style.backgroundColor='#ffffff'">
sssssssssssssssssssssssssss
</tr>
<%@ import Namespace="System.Data.OleDb" %>
<%@ import Namespace="System.Data" %>
<script runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
BindGrid()
End Sub
Sub BindGrid()
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source =" + Server.MapPath(".")+"/datagrid.mdb"
Dim myConnection As OleDbConnection
myConnection = New OleDbConnection(connstr) Dim myDataAdpater As New OleDbDataAdapter("Select Top 10 * from data", myConnection)
Dim myDataSet As DataSet
Try
myDataSet = New DataSet()
'----------------------------------
' Fill the DataSet for the DataBinding of the 2 DataGrid
'----------------------------------
myDataAdpater.Fill(myDataSet)
DataGrid2.DataSource = myDataSet
DataGrid2.DataBind()
Catch SQLEx As OleDbException : Response.Write(SQLEx.Message.ToString())
Catch Ex As Exception : Response.Write(Ex.Message.ToString())
End Try
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
'---------------------------------------------------
' Add the OnMouseOver and OnMouseOut method a Cell (Column) of DataGrid
'---------------------------------------------------
e.Item.Cells(1).Attributes.Add("onmouseover", "this.style.backgroundColor='#DDEEFF'")
e.Item.Cells(1).Attributes.Add("onmouseout", "this.style.backgroundColor='white'")
e.Item.Cells(2).Attributes.Add("onmouseover", "this.style.backgroundColor='#CC3300'")
e.Item.Cells(2).Attributes.Add("onmouseout", "this.style.backgroundColor='white'")
e.Item.Cells(2).Style("cursor") = "hand"
'---------------------------------------------------
' Change the Mouse Cursor of a particular Cell (Column) of DataGrid
' (Or you may do it for a whole Row of DataGrid :)
'---------------------------------------------------
e.Item.Cells(3).Style("cursor") = "hand"
'---------------------------------------------------
' Add the OnClick Alert MessageBox to a particular Cell (Column) of DataGrid
'---------------------------------------------------
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">
<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>
</form>
e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='seashell'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='#ffffff'");