ARTICLE

Viewing Data in a DataGrid Control in ADO.NET

Posted by Dinesh Beniwal Articles | ADO.NET in VB.NET May 07, 2010
In this article I will explain Viewing Data in a DataGrid Control in ADO.NET.
 
Reader Level:

The DataGrid control is a useful control in Web Forms. In this example I'll show you how to connect a DataGrid control a database table and view all the table's data in the DataGrid. 

First you create a Web application using the same steps you've used in two previous samples. Then you drag a button and data grid control onto the page. Change the Text property of the button to "Fill Data," change its properties, and add some to the page. The final Web page look like figure 7-18
 
Figure-7.18.jpg

 
Figure 7-18. Database application in ASP.NET 

 
The only thing you need to do now is write code to fill the data from a data base to the list box on the Fill Data button-click event handler. I'll use the OleDb data provider with the Access 2000 NorthWind database. Before using the OleDb data provider, though, don't forget to add a reference to the System.Data.OleDb namespace:
 
Imports System.Data.OleDb

 
Now write a click event handler for the button and write the code from Listing 7-3 on the handler.
 
Listing 7-3. Filling data from a database to the DataGrid

Imports System


Imports System.Collections


Imports System.Configuration


Imports System.Data


Imports System.Linq


Imports System.Web


Imports System.Web.Security


Imports System.Web.UI


Imports System.Web.UI.HtmlControls


Imports System.Web.UI.WebControls


Imports System.Web.UI.WebControls.WebParts


Imports System.Xml.Linq


Imports System.Data.OleDb

Namespace DataGrid


    Partial Public Class _Default


        Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)


        End Sub

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)


            ' Create  a Connection object


            Dim ConnectionString As String = " provider = Microsoft.Jet.OLEDB.4.0; " & "Data Source=C:/Northwind.mdb"


            Dim conn As New OleDbConnection(ConnectionString)

            ' open the connection


            If conn.State <> ConnectionState.Open Then


                conn.Open()


            End If

            ' Create a data adapter


            Dim da As New OleDbDataAdapter("SELECT CustomerID,CompanyName,Address,City FROM  customers", conn)

            ' Create and fill a dataset


            Dim ds As New DataSet()


            da.Fill(ds)

            ' Bind dataset to the control


            DataGrid1.DataSource = ds


            DataGrid1.DataBind()

            ' Close the connection


            If conn.State = ConnectionState.Open Then


                conn.Close()


            End If


        End Sub


    End Class

End Namespace

 

As you can see from Listing7-3, there is nothing new except one line: DataGrid.DataBind(). I'll discuss the DataBind method in a moment. You'll follow the same steps to create a connection and data adapter objects as you've been doing in the previous articles of the site. In this example, I'm using The Access 2000 database, C:\\Northwind.mdb, and the OleDb data adapter to connect to the database. After creating a connection and data adapter, create and fill a dataset by calling to the data adapter's Fill method. After that, set dataset as the DataGrid control's Data Source property and call DataGrid's DataBind method.

Now Compile and run the project. The output of the Fill Data button looks like figure 7-19.

 
Figure-7.19.jpg

 
Figure 7-19. The output of clicking the Fill Data button

 
Neat, huh? How easy are writing database Web applications using ADO.NET?

 
Conclusion

 
Hope this article would have helped you in understanding Viewing Data in a DataGrid Control in ADO.NET. See other articles on the website also for further reference.

Login to add your contents and source code to this article
share this article :
post comment
 

How can I display only selected columns from a dataset to a datagrid.

Posted by Shree Aug 16, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Diagram
Become a Sponsor