Sunday, February 6, 2011

DataGrid, DataList and Data Repeater

There is a lot of documentation on each of these controls. I thought of presenting a quick and concise summary explaining the differences between these 3 controls. Ofcourse there is detailed article on the subject by Scott Mitchel on MSDN.

Essentially all the three i.e. DataGrid, DataList and Repeater controls are used to represent repetitive data in a suitable manner to the users. Also data is bound to these controls in same manner simply by specifying the datasource (an IEnumerable one) and calling the DataBind() method on the control.

While DataGrid and Datalist inherit from WebControl; Repeater control inherits from Control class itself.

DataGrid represents data in a tabular manner with each datarow represented as a "tr" in a "table" and each column is represented as a "td" in a row. There is very little flexibility here except that the user can define what type of column to represent data i.e. button; link; label, etc.
But though the flexibility is low; a lot of out-of-box functionality is available via DataGrid i.e Sorting, Paging, Editing etc with little or practically no custom coding.

DataList too represents data inside a table but gives more flexibility than DataGrid. It can display more than one datarows inside a table row "tr"; it provides many more templates for defining your own custom HTML output; instead of table, data can be represented using a span tag.
But the freedom comes at a price. For sorting; paging as well as editing data rows, much more custom code needs to be written by the developers.

Repeater control gives the maximum flexibility, there is not default HTML output but there are numerous templates from header to almost every item that the developer can use to emit desired HTML. In one sense a complete freedom to define your own output but again at a cost of huge development time for any of the desired features.

So in summary use the control that gives you the functionality you desire but also keep in mind the performance. DataGrid makes heavy use of ViewState and hence performance is least compared to other two. While repeater makes practically no use of it and hence it’s performance is the best.

No comments:

Post a Comment