DataGrid control in ASP.NET

The DataGrid Server control is a multicolumn, data-bound grid that enables you to define different types of columns. These columns not only provide layout to the grid contents, but also add functionalities to select, edit, sort, and page the data. Also, the DataGrid control provides functionality for full customization of the output through the use of specific columns called TemplateColumns.

When you add a DataGrid control to a form, the control will be rendered only if it is bound to a data source. A data source can be created by using Visual Studio Data Designer or any database on a server. It can also be a simple structure, such as an array of type ArrayList. When you bind a DataGrid control to a data source, the columns are generated automatically based on the fields in the data source. However, you can specify the columns in different ways. The different types of columns that you can create are described as follows:

Bound columns: Used to specify the database fields that need to be displayed in the columns along with the order, format, and style of the display.

Hyperlink column: Used to display information as hyperlinks that users can click to navigate to different pages. For example, you can display product names as hyperlinks. When users click these hyperlinks, they are directed to product details in a separate page.

Button columns: Used to display buttons for each item in the grid and add custom functionality to the buttons. For example, you can add a button labeled Query and associate logic with it. When users click the button, the logic is executed.

Edit command columns: Used to allow in-place editing of items. Inplace editing allows users to edit the items in the grid only. To implement this editing, you need to add a special column in the grid that contains buttons labeled Edit. When users click this button, the current row is
displayed again with editable fields for all columns. Also, the column with the Edit button is redisplayed with the Update and Cancel buttons.

Template columns: Used to create a custom layout for a column by using a combination of HTML text and Server controls. The controls in the Template column can be bound to display data from a data source. This column provides you complete flexibility for customization. With the template columns, you can create custom-editing layout. This approach enables you to manipulate the columns that can be edited and the way users can edit data.

The DataGrid control supports multiple events that are raised from the controls in the grids. In addition to the ItemCreated, ItemCommand, EditCommand, UpdateCommand, CancelCommand, and DeleteCommand events, the control supports these two events:

PageIndexChanged: Generated when a page selection element is clicked.

SortCommand: Generated when a column is sorted.

Comparing the Repeater, DataList, and DataGrid controls

As mentioned, the Repeater, DataList, and DataGrid controls are used to display data in the form of lists on the Web Forms pages. All of these controls are bound to some data source and display each row in the data source as an entry called item.

All three controls must be bound to a data source by the DataSource property so that they can be rendered on a page. Even if the DataSource property is set, the controls will not display the data from the data source until the DataBind() method is called for the control. The DataSource property is set for the control. For the individual controls in the templates, you can use the container data.

Creating Templates

You can create templates by using the Web Forms Designer in a WYSIWYG (What You See Is What You Get) way or by using the ASP.NET syntax in the ASPX file. This section explores both of these methods.

The method to create templates by using the Web Forms Designer is very convenient to use:

1. Right -click the control and choose Edit Template from the shortcut menu.

Then, choose the type of template to edit. For example, to edit Item templates, such as ItemTemplate, AlternatingItemTemplate, SelectedItemTemplate, or EditItemTemplate of a DataList control, choose Item Templates. The control appears in the template-editing
mode.

2. In the template-editing mode, add the HTML text and drag controls from the Toolbox onto the template. For example, you can drag Label or TextBox control to the Item template.

3. Edit the properties of the controls that you have added as you would do for any other control.

4. After you have added all the controls that you need to the template and edited their properties and styles, you need to end the template editing.

To do so, right-click the control and choose End Template Editing.

You can also create a template by directly editing the ASPX file in the HTML view.

You can then add the controls that you want to include in the Itemtemplate. Also, you can set the properties for the embedded controls and data-bind them.

In this syntax, data binding expression represents the data binding expression for the Label control of the template. The data binding expression is evaluated at run time. As mentioned earlier, the Server control for which the template is created must be bound to a data source. Otherwise, it will not be rendered on the page.

Then, the controls within the template can be bound to the container data. The DataBinder.Eval() method provided by the .NET Framework evaluates the late-bound data binding expression and eliminates the explicit casting of the bound data to the desired data type .The DataBinder.Eval() method takes three arguments and has the following syntax:

<% # DataBinder.Eval(NamingContainer,DataFieldName,[FormatString])>

In this syntax:

NamingContainer: Represents the naming container for the data item. The Page object can be one of the containers for data items. For the Repeater, DataList, and DataGrid controls, this argument is always Container.DataItem.

DataFieldName: Represents the name of the data item field.

[FormatString]: Represents the format string, which determines the format of the bound data item. This is an optional argument. If this argument is not specified, the data item is automatically formatted to a value, which is of object type.

Combining templates with the Repeater control

The templates that the Repeater control supports include ItemTemplate, AlternatingItemTemplate, HeaderTemplate, FooterTemplate, and SeparatorTemplate. To
render the Repeater control on a page, you must bind the control to a data source and create at least ItemTemplate. The controls within the templates can be bound to the container data.
This section implements creating templates for the Repeater control.

First, create an ASP.NET Web application project. You can create a Visual Basic or C# application. The example in this section creates a Visual Basic Web application. After you create the application, add a Repeater control to the form. In the example that follows, the Repeater control is bound to a DataView object, and the individual items are bound to the individual fields in the data source. To use the Data objects, you need to import the System.Data namespace.

Combining templates with the DataList control

The DataList control supports two more templates in addition to the templates supported by the Repeater control:

SelectedItemTemplate: Enables users to select items

EditItemTemplate: Enables users to edit items

You must bind the DataList control to a data source and create at least ItemTemplate to render the control on a page. To understand templates with the DataList control, you'll use the same data source as you used for the Repeater control. First, you need to add a DataList control to a form.

related post

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO


No comments:

Post a Comment