The DataGrid Web server control is a multi-column, data-bound grid.
Columns can be made that displays and edit data. Multi-columns include Edit, Update, Cancel,
Select buttons, Custom Buttons, and Template Columns. Therefore Template Columns can
be laid further in Template-Editing Mode.
The DataGrid control displays the fields of a data source as columns in a table. Each row in the control represents a record in the data source. The control supports selection, editing, deleting, paging, and sorting.
The DataGrid control with strong features is the most complicated control included within the ASP.NET framework. Like the Repeater and DataList controls, it enables to format and display records from a database table. However, it has several advanced features, such as support for sorting and paging through records, which makes it unique.
Records can be displayed in a DataGrid without using templates. A data source can be simply bound to the DataGrid, and it automatically displays the records.
The following example, displays all the records from the Employees database table in a DataGrid
Example 43 ExpertDataGrid.aspx |
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Page_Load
Dim conNorthwind As SqlConnection
Dim cmdSelect As SqlCommand
conNorthwind=New SqlConnection( "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>ExpertDataGrid.aspx</title></head>
<body>
<asp:DataGrid
ID="dgrdEmployees"
Runat="Server" />
</body>
</html> |
| |
The output of above example is shown below:

By default, a DataGrid displays gridlines around its items. Modification of the Grid line appearance can be done by setting the GridLines property. The possible values are Both, Horizontal, None, or Vertical.
For example, to completely disable GridLines, the DataGrid would look like this: -
<asp:DataGrid
GridLines="None"
Runat="Server" /> |
| |
The cell spacing and cell padding of the cells in a DataGrid can be controlled by modifying the DataGrid control's CellSpacing and CellPadding properties like this:
<asp:DataGrid
CellSpacing="10"
CellPadding="10"
Runat="Server"> |
| |
A background image can be specified for a DataGrid by assigning the name of an image to the BackImageUrl property.
For example, the following DataGrid displays an image named Bricks. Gif in the background:
<asp:DataGrid
BackImageUrl="expert.Gif"
Runat="Server" /> |
| |
Finally, headers and footers can be displayed and hidden for the columns in a DataGrid by enabling or disabling the ShowHeader and ShowFooter properties. By default the ShowHeader property has the value True, and the ShowFooter property has the value False. To prevent column headers from being displayed, a DataGrid would be like this:
<asp:DataGrid
ShowHeader="False"
Runat="Server" /> |
| |
|