Web Studyaspnet.blogspot.com Babyneed.blogspot.com

ASP.Net Web Developer Guide

Power By: eXTReMe Tracker Powered by Blogger  
 
 
 
 

 

 
Important Links
 
   
     
 
Sitemap
 
   
   
 
Reference
 
   

Tuesday, March 28, 2006

Displaying Data in a DataGrid Control Using Default Column Mapping

In this example, we will use the default layout of a data grid to display the bound data.The expected output of this example is shown in Figure 3.76. Exactly like a Repeater, or a DataList control, the DataGrid control also requires binding to an appropriate data source. Besides the binding chore, the specification of the data grid, particularly in this example, is extremely simple as follows:
<asp:DataGrid id="dataGrid1" runat="server" />
Figure 3.76 Displaying Data in a DataGrid Control

The complete listing of the application is shown in Figure 3.77.
Figure 3.77 DataGrid1.aspx
<!— Chapter3/DataGrid1.aspx —>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html><head></head>
<script language="VB" Debug="true" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
If Not IsPostBack Then
bindListControl
End If
End Sub
Sub bindListControl()
Dim myConn As OleDbConnection
Dim myOleDbAdapter As OleDbDataAdapter
Dim connStr, sqlStr As String
Dim myDataSet As New Dataset
connStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Products.mdb"
sqlStr="SELECT ProductId, ProductName, Price " _
+ "FROM Products WHERE Price > 40 ORDER BY Price"
myConn= New OleDbConnection(connStr)
myConn.Open()
myOleDbAdapter =New OleDbDataAdapter(sqlStr,myConn)
myOleDbAdapter.Fill(myDataSet,"dtProducts")
DataGrid1.DataSource=myDataSet.Tables("dtProducts")
DataGrid1.DataBind()
myConn.Close()
End Sub
</script>
<body bgcolor="white">
<asp:DataGrid id="dataGrid1" runat="server" />
</center></body></html>

0 Comments:

Post a Comment

<< Home