|
The GridView Control will display the data in the Grid format. The fields are bound with the columns of the Grid.
Columns in GRID VIEW Control:
The following types are supported by the GridView while bounding the columns with it.
- BoundField: Default column that will display records.
- HyperlinkField: Display records as hyperlink.
- TemplateField: Display records using a template.
- ButtonField: Display Button controls.
- CommandField: Display commands.
- ImageField: Display images.
Templates that are used to format the output of the control.
- HeaderTemplate formats the header for DataList
- ItemTemplate formats each item in the DataList
- FooterTemplate formats footer.
- EditItemTemplate controls how an item selected for editing is formatted.
Example for GridView control
Note:- In example the grid view control will display the first name, last name and social security number of the employee in the Bound column of the grid view control.
GridViewControl.aspx
<% @ Page Language ="VB" AutoEventWireup ="false" CodeFile ="Example11.aspx.vb" Inherits ="Example11" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns ="http://www.w3.org/1999/xhtml" >
<head runat ="server">
</head>
<body>
<form id ="form1" runat ="server">
<div>
<asp:GridView ID ="gvEmployee" runat ="server" AutoGenerateColumns ="false" BorderColor ="Blue" DataKeyNames ="emp_id" Width ="490px">
<Columns>
<asp:BoundField DataField = "fname" HeaderText ="FirstName"/>
<asp:BoundField DataField = "lname" HeaderText ="LastName"/>
<asp:BoundField DataField = "emp_id" HeaderText ="Soc-Sc-No"/>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
GridViewControl.aspx.vb
Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class Example11 Inherits System.Web.UI.Page
Protected Sub Page_Load( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Load
Dim con As SqlConnection
Dim ada As SqlDataAdapter
Dim dst As DataSet
con = New SqlConnection( "Data Source=localhost;Initial catalog=pubs;UID=sa;pwd=sa" )
ada = New SqlDataAdapter( "select * from Employee" , con)
dst = New DataSet
con.Open()
ada.Fill(dst, "Employee" )
con.Close()
gvEmployee.DataSource = dst.Tables( "Employee" )
gvEmployee.DataBind()
End Sub
End Class |