| |
Creating Columns in a DataGrid Control
The DataGrid control displays the columns in a variety of ways.
By default, the columns are generated automatically based on fields in the data source. However, in order to control the content and layout of columns more precisely, the following types of columns can be defined:
| Type of Column |
Description |
| Bound column |
Allows specifying which data source field to display and specifies the format of that field, using a .NET formatting expression. For details see Adding Bound Columns to a DataGrid Web Server Control. |
| Hyperlink column |
Displays information as hyperlinks. A typical use is to display data (such as a customer number or product name) as a hyperlink that users can click to navigate to a separate page that provides details about that item. For details see Adding Hyperlink Columns to a DataGrid Web Server Control. |
| Button column |
Allows adding a button for each item in the grid and defining custom functionality for that button. For example, you might create a button labeled "Add to Shopping Cart" that runs your custom logic when a user clicks it. You can also add predefined buttons for Select, Edit, Update, Cancel, and Delete functions. |
| Edit, Update, Cancel column |
Allows creating in-place editing. For more details, see "Editing Items" below. |
| Template column |
Allows creating combinations of HTML text and server controls to design a custom layout for a column. The controls within a template column can be data-bound. Template columns gives great flexibility in defining the layout and functionality of the grid contents, because you have complete control over how the data is displayed and what happens when users interact with rows in the grid. For details see Adding Template Columns to a DataGrid Web Server Control. |
Events
The DataGrid control supports several events.
One of them, the ItemCreated event, gives you a way to customize the item-creation process. The ItemDataBound event also gives you the ability to customize the DataGrid items, but after the data is available for inspection. For example, if you were using the DataGrid control to display a to-do list, you could display overdue items in red text, completed items in black text, and other tasks in green text.
The remaining events are raised in response to button or LinkButton clicked in grid items. They are designed to implement common data manipulation tasks. Four events of this type are supported:
- EditCommand
- DeleteCommand
- UpdateCommand
- CancelCommand
When the user clicks one of the buttons (labeled by default Edit, Delete, Update, or Cancel, respectively), the corresponding event is raised.
The DataGrid control also supports the ItemCommand event that is raised when a user clicks a button that is not one of the predefined buttons above. This event can be used for custom functions by setting a button's CommandName property to a value needed, and then testing for it in the ItemCommand event handler.
(For example, you could use this approach when selecting an item, as documented in allowing Users to Select Items in a DataList Web Server Control.) By default, a DataGrid simply displays all the columns from its data source. However, if False value is assigned to the DataGrid control's AutoGenerateColumns property, columns can be created individually to have more control over the formatting.
Adding a BoundColumn to a DataGrid
The default column used in a DataGrid is a BoundColumn. If only limited columns are to be displayed and controlled from a data source, declaration of one or more BoundColumns controls is done explicitly.The following example demonstrates it :-
| Example 44 ExpertBoundColumns.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>ExpertBoundColumns.aspx</title></head>
<body>
<asp:DataGrid
ID="dgrdEmployees"
AutoGenerateColumns="False"
EnableViewState="False"
Runat="Server">
<Columns>
<asp:BoundColumn DataField="FirstName" />
<asp:BoundColumn DataField="LastName" />
</Columns>
</asp:DataGrid>
</body>
</html>
|
| |
The output of above example is shown below:

|