Search - Articles - Dev Forums - Favorites - Member Login
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009


Dev Articles
Search Directory
ESET
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
Using a Bound Column in a DataGrid Control

Author: Faraz

This article will shows you, how to display a record in a DataGrid using a BoundColumn control. Here I will display the Department’s record from an Access Database. Read on to learn more!

If you need to display a list of records of data, probably the easiest control to use is the DataGrid. The DataGrid control can be set up so that it automatically displays all the fields and records in a bound table without having to specify any information. The resulting rendered HTML output is a highly structured HML table.

 

You can let a DataGrid control automatically display the columns in the bound records but you may find that you need to have more controls over the format of the data. For examples, when you let the DataGrid control automatically display the columns, the headers for the columns come from the names of the fields that they are displaying, and sometimes field names are not very readable to the end-user. 

 

Another problem with letting the DataGrid define columns for you is that the DataGrid does not provide much formatting for the dates and numbers. To solve both these problems, you need to define your own columns. The technique in this article will show you how to create BoundColumn controls within a DataGrid control.

 

To start with this project selects the New Project option from the Startup page. Add the Label control, define its properties (id="lblDisplay" Font-Size="12pt" Font-Bold="True" Font-Name="Lucida Console" text="Department Details" Runat="server"). Define a DataGrid control, set its properties (id="dgDept" Runat="server" AutoGenerateColumns="False") and finally in a DataGrid control define the BoundColumn controls set their properties (HeaderText, DataField, DataFormatString). 

 

Note:

·        The number of BoundColumn controls (means 1, 2…n) depends on the number of columns you want to display from the database in a DataGrid control.

·        You can use the DataFormatString for displaying dates and expenses in specific formats.

 

When the page loads, code that populates the records bound to the DataGrid control runs:

 

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Put user code to initialize the page here

        If Not IsPostBack Then

            Dim DBConn As OleDbConnection

            Dim DBCommand As OleDbDataAdapter

            Dim DSPageData As New DataSet

            DBConn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" & "DATA SOURCE=" & Server.MapPath("db1.mdb;"))

            DBCommand = New OleDbDataAdapter("SELECT DeptName, DeptEmail, AnualExpences From Departments Order By DeptName", DBConn)

            DBCommand.Fill(DSPageData, "Departments")

            dgDept.DataSource = DSPageData.Tables("Departments").DefaultView

            dgDept.DataBind()

        End If

    End Sub

 

The code only runs when the page first loads:

 

              If Not IsPostBack Then

 

If that is the case you need the data objects:

               

            Dim DBConn As OleDbConnection

            Dim DBCommand As OleDbDataAdapter

            Dim DSPageData As New DataSet

 

You start by connecting to the Database:

 

    DBConn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" & "DATA SOURCE=" & Server.MapPath("db1.mdb;"))

 

And you retrieve the Department data:

 

      DBCommand = New OleDbDataAdapter("SELECT DeptName, DeptEmail, AnualExpences From Departments Order By DeptName", DBConn)

      DBCommand.Fill(DSPageData, "Departments")

 

 

This bound to the DataGrid control:

 

      dgDept.DataSource = DSPageData.Tables("Departments").DefaultView

      dgDept.DataBind()
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Creating a DropDownList control in code

Disclaimer - Privacy
© 2002-2010 DevASP.net