The above example, displays only the FirstName and LastName columns from the Employees table.
Note that the DataGrid control's AutoGenerateColumns property is set to the value False so that, all the columns from the Employees table are displayed, and the FirstName and LastName columns are displayed twice as shown below: -

Therefore it is important to take care of AutoGenerateColumns property of DatGrid control.
Further in the above example the BoundColumns are declared inside the DataGrid control's <Columns> tag. This tag contains a BoundColumn for the Titles column and a BoundColumn for the Price column. The DataField property indicates the field from the data source to display in the BoundColumn.
Adding a HyperLinkColumn to DataGrid Control
This is a very important concept in DataGrid Controls. To display links to other pages from the DataGrid, a HyperLinkColumncan be used.
This type of column is useful in two situations.
First, a HyperLinkColumn can be used, for example create a master-detail form. A set of master records can be displayed in a DataGrid and a HyperLinkColumn can be used in the DataGrid to link to a separate page that contains detailed information.
Secondly it can be used for interlinking pages and websites with each other, an important need of web programmers.
For example, you can use a HyperLinkColumn to display a list of useful ASP.NET Web sites.
| Example 45 ExpertDataGridHyperLink.aspx |
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Page_Load
Dim conNorthwind As SqlConnection
Dim cmdSelect As SqlCommand
conNorthwind=NewSqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Northwind" )
cmdSelect = New SqlCommand( "Select * From Employees", conNorthwind )
conNorthwind.Open()
dgrdEmployees.DataSource = cmdSelect.ExecuteReader()
dgrdEmployees.DataBind()
conNorthwind.Close()
End Sub
</Script>
<html>
<head><title>ExpertDataGridHyperLink.aspx</title></head>
<body>
<form Runat="Server">
<asp:DataGrid
ID="dgrdEmployees"
AutoGenerateColumns="False"
EnableViewState="False"
CellPadding="10"
Runat="Server">
<Columns>
<asp:BoundColumn
HeaderText="Employee"
DataField="FirstName" />
<asp:HyperLinkColumn
HeaderText="Complete Details"
DataNavigateUrlField="EmployeeID"
DataNavigateUrlFormatString="EmpDetails.aspx?id={0}"
Text="view complete details" />
</Columns>
</asp:DataGrid>
</form>
</body>
</html> |
| |
|