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:
The code only runs when the page first loads:
If that is the case you need the data objects:
You start by connecting to the Database:
And you retrieve the Department data:
This bound to the DataGrid control: