- GridView provides a tabular grid-like view of the contents of a data source
- The GridView is designed to leverage the new data source object model, and it works best when bound to a data source control via the DataSourceID property
- A Grid can have delcared and auto generated columns at the same time
- Auto-generated columns are not added to the Columns collection
- The GridView doesn't support the AllowCustomPaging property found in DataGrid
- Compared to the DataGrid control, the GridView provides an extended eventing model; pre/post pairs of events, with cancelling and more
- It is declared as:
public class Gridview : CompositeDataBoundControl, IcallbackContainer, ICallbackEventHandler
Properties of GridView Control
AllowPaging, AllowSorting | Support for Paging, Sorting |
AutoGenerateColumns | Default is true |
AutoGeneratexxxButton | Delete, Edit and Select buttons can be displayed with each reord |
DataMember | Specifies which table to be bind from the DataSource |
DataSource/DataSourceID | Indicates the bound data source control |
EnableSortingAnd PagingCallbacks | |
SortDirection | |
SortExpression | |
UseAccessibleHeader |
- GridView has some Style properties like AlternatingRowStyle, EditRowStyle, HeaderStyle, FooterStyle, PagerStyle, RowStyle, SelectedRowStyle
- GridView appearence properties include BackImageUrl, Caption, GridLines, PagerSettings, ShowFooter
- Templating properties are EmptyDataTemplate and PagerTemplate
- State Properties include: DataKeyNames, DataKeys, PageCount, SelectedIndex, SelectedRow, SelectedValue
Events Fired by the GridView Control
PageIndexChanging, PageIndexChanged | RowDeleting, RowDeleted |
RowCancellingEdit | RowEditing |
RowCommand | RowUpdating, RowUpdated |
RowCreated | SelectedIndexChanging SelectedIndexChanged |
RowDataBound | Sorting, Sorted |
Configuring Columns
- Columns can Programatically be defined as:
BoundField field = new BoundField();
Field.DataField = "companyname";
Field.HeaderText = "Company Name";
grid.ColumnFields.Add(field);
- Columns can be Declaratively defines as:
<columns>
<asp:boundfield datafield="customerid" headertext="ID"/>
</columns>
Column Types in GridView
- GridView supports following types of columns
- BoundField
- ButtonField, CheckBoxField
- CommandField
- HyperLinkField, ImageField, TemplateField
- BoundField
Templated Fields
- A TemplateField column gives each row in the grid a personalized UI that is completely defined by the page developer
- Templates can be defined for various rendering stages, including the default view, in-place editing, header etc
- Declared as:
<asp:templateField headertext ="Product" >
<itemTemplate>
<b><%# Eval("productname")%></b> <br/>
available in <%# Eval("quantityperunit")%>
</itemtemplate>
</asp:templateField>
Sorting Data
- The GridView doesn't implement a sorting algorithm; instead, it relies on the data source control (or the page, if bound to an enumerable object) to provide sorted data
- To enable the GridView's sorting capabilities, set AllowSorting property to true
Executing an Operation on a Given Row
- A RowCommand event handler is required to handle row operations
- GridView stores the index of the clicked row in the CommandArgument property of the GridViewCommandEventArgs structure
No comments:
Post a Comment