Using the AccessDataSource Control in VS 2005 - Binding the query result to a GridView
(Page 3 of 4 )
GridView is an improvement over the DataGrid in many ways. This tutorial does not cover all the nice things about GridView, but simply uses it to display the result of running the query. At this point you may add a Command button and a GridView control to the default.aspx page as shown. If you click the little black arrow at the top left of the GridView Control (pointing backwards) you may get a hint as to the tasks you need to perform for this control. If you want you may choose the data source, in which case you need not write any code. Here, no source has been defined in the design mode. Only the Auto Format... hyperlink was clicked to make GridView look pretty.

Binding data to a GridView Control
To the click event of the button marked Select employees Information, type in the following code.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As _
System.EventArgs)
Handles Button1.Click
Dim ds As New AccessDataSource
ds = AccessDataSource1
Response.Write(ds.CacheDuration & "<br>")
Response.Write(ds.CacheExpirationPolicy & "<br>")
Response.Write(ds.ConnectionString.ToString & "<br>")
ds.DataFile = "~/App_Data/nwind.mdb"
ds.DataSourceMode = SqlDataSourceMode.DataSet
GridView1.DataSource = ds
GridView1.DataBind()
GridView1.Visible = True
End Sub
The code instantiates a new AccessDataSource control and assigns to it the configured control described earlier. The three statements write the CacheDuration, CacheDurationPolicy and the ConnectionString to the screen. These are not necessary for data binding, but written out of curiosity. However, the DataFile and DataSourceMode are necessary for the display. The binding of DataSource to GridView uses just one line of code. When you build the application and choose to display the default.aspx page by clicking on the Show employees Information button, you will see the following.

Next: Binding data to a drop-down list control >>
More Database Code Articles
More By Jayaram Krishnaswamy