Monday, November 26, 2012

Gridview



ASP.NET Data-Bound Web Server Controls Overview
Data-bound Web server controls are controls that can be bound to a data source control to make it easy to display and modify data in your Web application.

Data-bound Web server controls are composite controls that combine other ASP.NET Web controls, such as Label and TextBox controls, into a single layout.

In addition to enabling you to bind the control to a set of data results, data-bound controls enable you to customize the layout of the control using templates.

Binding a Data-bound Web Server Control to Data

You can work with a data-bound control by binding it to a data source control such as an ObjectDataSource or SqlDataSource control.

 The data source control connects to a data source such as a database or middle-tier object and then retrieves or updates data. The data-bound control can then use this data.

GridView Control

The GridView control displays data as a table and provides the capability to sort columns, page through data, and edit or delete a single record.

Using fields with the GridView control

Note that these are the only fields which we can use in a GridView control. 


FieldDescription
asp:BoundFieldEnables us to display the value of a data item in a data source as text
asp:ButtonFieldEnables us to display a button or the value of a data item as a button
asp:CheckBoxFieldEnables us to display the value of a data item as a check box
asp:CommandFieldDisplays select, edit, delete, update or cancel buttons
asp:HyperLinkFieldDisplays a hyperlink
asp:ImageFieldDisplays an image
asp:TemplateFieldEnables customization of the appearance of a data item

Create Templatefields in a GridView control

We use TemplateFields, when we wish to display ASP.Net controls in a GridView column, to provide additional functionality in the user interface. 
 A Template field supports many types of templates and a list of template types is given in the table below. 

TemplateTypeDescription
AlternatingItemTemplateThe contents of this template are displayed for every other row rendered by the GridView
EditItemTemplateThe contents of this template are displayed when a row is selected for editing
FooterTemplateThe contents of this template are displayed in the column footer
HeaderTemplateThe contents of this template are displayed in the column header
InsertTemplateThe contents of this template are displayed when a new data item is inserted
ItemTemplateThe contents of this template are displayed for every row rendered by the GridView

We can create TemplateFields in the GridView control using <TemplateField> element. 

Steps to create the <TemplateField> element in the GridView control 

a. Declare the GridView and set the AutoGenerateColumns property to ‘false’. 
b. Create a Template column using <asp:TemplateField> tag within the <Columns> element.
Create within the <asp:TemplateField> element to display value of field as text.
Create <EditItemTemplate> to display TextBox control to modify value of field when editing the record.


DetailsView Control

The DetailsView control renders a single record at a time as a table and provides the capability to page through multiple records, as well as to insert, update, and delete records. 

The DetailsView control is often used in master-detail scenarios where the selected record in a master control such as a GridView control determines the record displayed by the DetailsView control.

For more information, see DetailsView Web Server Control Overview.

FormView Control

The FormView control renders a single record at a time from a data source and provides the capability to page through multiple records, as well as to insert, update, and delete records, similar to the DetailsView control.

However, the difference between the FormView and the DetailsView controls is that the DetailsView control uses a table-based layout where each field of the data record is displayed as a row in the control.

 In contrast, the FormView control does not specify a pre-defined layout for displaying a record. Instead, you create templates that contain controls to display individual fields from the record.

 The template contains the formatting, controls, and binding expressions used to lay out the form.
You can specify whether the FormView control renders content using tables by setting the RenderTable() property to true.

For more information, see FormView Web Server Control Overview.

Repeater Control

The Repeater control renders a read-only list from a set of records returned from a data source.
 Like the FormView control, the Repeater control does not specify a built-in layout. Instead you create the layout for the Repeater control using templates.
For more information, see Repeater Web Server Control Overview.

DataList Control


The DataList control renders data as table and enables you to display data records in different layouts, such as ordering them in columns or rows.

The DataList control does not take advantage of the capabilities of data source controls for modifying data; you must supply this code yourself.

 The DataList control differs from the Repeater control in that the DataList control explicitly places items in an HTML table, where as the Repeater control does not.

For more information, see DataList Web Server Control Overview.

ListView Control

The ListView control displays data from a data source in a format that you define using templates.

The template contains the formatting, controls, and binding expressions that are used to lay out the data. 

The ListView control is useful for data in any repeating structure, similar to the DataList and Repeater controls.

However, unlike the DataList andRepeater controls, the ListView control implicitly supports edit, insert, and delete operations, as well as sorting and paging functionality.


Comparing ListView with GridView,DataList and Repeater

The ListView control is a new data presentation control that was added in ASP.Net 3.5.You may wonder why its added to the framework , and what it provide .
From what i have seen, ListView control was added to provide the following functionalities:
A very flexible and customizable layout.
A built in data paging support with the new DataPager control.
Support data grouping (repeating items) in a flexible way.
Built in support for deleting,inserting,paging,sorting,and updating the data.
Now , to compare the ListView control with the dataList,GridView and repeater control , lets look at the table below :
Supported Funcationalities
Control
Paging
Data Grouping
Provide Flexible Layout
Update,Delete
Insert
Sorting
ListView
supported
supported
supported
supported
supported
supported
GridView
supported
Not supported
Not Supported
supported
Not Supported
supported
DataList
Not supported
supported
supported
Not supported
Not supported
Not supported
Repeater
Not supported
Not supported
supported
Not supported
Not supported
Not supported

* Supported: means that it’s provided out of the box without any custom code or hacks.
* Not Supported: means that it’s not provided out of the box by the control but it could be possible to implement it using custom code \ hacks.
The GridView : it supports paging but it doesn’t provide a flexible layout , since its mainly used to display the data in a table based layout.And If we looked at data inserting , the Gridview doesn’t have a built in support for inserting data( since it doesn’t call the insert method of it underlying data source when you click on a button with a CommadName set to “Insert” ).
The DataList : it support data grouping ( through its RepeatColumns property) , but it doesn’t have a built in support for paging,inserting ,deleting , updating the data. and if you looked at its laout , you will find that by default  the datalist renders as html table and you will have to set its flowLayout to “Flow” to stop that behaviour.
The Repeater control : you will find that it provides a flexible layout but it doesn’t support data grouping ,inserting,deleting , updating  and paging through the data .
Summary :
The ListView control was added to provide a rich data control that can support all the required functionalities at the same time , so now you can easily display a fully customizable layout that supports Grouping,paging , inserting , deleting , updating and sorting the data.

 

No comments:

Post a Comment