Datagrid Vs Datalist Vs Repeater
One of the most popular questions in the .Net world is "What's the difference between the Datagrid, the Datalist and the Repeater? And why would you choose one over the others?"
Though this question has been answered in hundreds of blogs and articles online, some of us still consider this to be a grey area and leave it mostly to the developer's preference.
Well...here's what i understand about these web controls.
First of all, all the three controls were created with the same objective, which is to display data. So why are there 3 different controls?
When these controls are bound to a datasource, for each entity in the datasource an "item" is created and added to the collection of items for the control.
Each Webcontrol's "item" belongs to a different class. For example, a DatagridItem class is derived from TableRow class. This is because the HTML rendered by a datagrid is nothing but an HTML table with rows and columns based upon the datasource it was bound to. Hence each item in a datagrid collection is nothing but an HTML table row, when it is displayed in the browser.
One big advantage of the datagrid is that it is very simple to use and easily customizable with some cool color settings for borders,backgrounds etc. It also supports paging and sorting which also is accompanied by some customizable features for example, the way the page index can be displayed, as numbers or "previous and next" links etc.
So if you have a source of data that needs to be displayed with no customization or no complex stuff, datagrid is the best choice.
But remember, you will have no control over the HTML table that is being rendered.
Like the rows, each column in a DataGrid is an instance of a class that is derived from the DataGridColumn class. There are five built-in DataGrid column types:
* BoundColumn
* ButtonColumn
* EditColumn
* HyperLinkColumn
* TemplateColumn
Besides these built-in data types we can also create our own customized DataGrid column types by creating a class that derives from the DataGridColumn class.
Most of the newbies out there do not pay attention to the most important issue in datagrids ; Viewstate. ViewState produced by the DataGrid can be humongous if it contains a considerably large number of rows and thus be detrimental to performance. Of course, you DO have the option to turn the viewstate off but then that doesn't help you if you are planning to use sorting, paging and editing features in the datagrid.
Apparently the datagrid has the worst performance of all the 3 web controls.
The Repeater Web control offers the most flexibility as far as control of the rendered HTML is concerned. The datagrid and datalist controls place the content within a HTML tag like <table> or <span> automatically. That's how they are rendered. But a Repeater control renders ONLY what is specified by the developer. For this reason, if you wish to display data in some way other than in an HTML <table> tag or in a series of <span>tags, Repeater control is the best choice.
A Repeater is designed to let the user customize its output and thus have complete control over what is being rendered. So understandably RepeaterItem class is not derived from the TableRow class.
Just like in the DataList, with Repeater you specify the markup using templates. The Repeater contains the following five templates:
* AlternatingItemTemplate
* FooterTemplate
* HeaderTemplate
* ItemTemplate
* SeparatorTemplate
An important thing to remember about the Repeater is that, it is not derived from the WebControl class, like the DataGrid and DataList due to which it lacks the stylistic properties common to both the DataGrid and DataList. So if you want to make things look pretty in a repeater, write it yourself ;). But in most cases this is what i want : Complete control over what is being rendered.(Yupp... i AM a control freak!)
Among all the 3 web controls in question, Repeater offers the best performance, though only slightly ahead of the datalist but beating datagrid by a considerable margin.
Finally about the datalist.
The DataList does not use columns as in datagrid.It uses templates to display items just like Repeaters. The advantage of using a template is that with a template, you can specify both a mix of HTML content and databinding syntax.
Along with the ItemTemplate the DataList supports six other templates for a total of seven:
* AlternatingItemTemplate
* EditItemTemplate
* FooterTemplate
* HeaderTemplate
* ItemTemplate
* SelectedItemTemplate
* SeparatorTemplate
By default, the DataList displays each item as an HTML tablerow. But an important property of datalist is the RepeatColumns property. Using this property you can specify how many DataList items should appear per table row. In addition to that, you can also specify if you want the contents of the DataList should be displayed using <span> tags instead of a <table> tags. This is done by setting the RepeatLayout property, to either Table or Flow, which would render the data in HTML <table> tags or in <span> tags.
With its templates and RepeatColumns and RepeatLayout properties, it's obvious that the DataList allows for much more customization of the rendered HTML markup than the DataGrid. Thus customization again, is the reason why one might prefer to use a datalist over datagrid.
One major advantage of using Datagrid over datalist and repeater is, as already mentioned, the sorting and paging functionality. While such functionality can be implemented with some smart logic in the code, it's the question of how much time and effort would be involved in doing it and whether the trade-off is worth it in the application development process.
Guess ...that's about it. This is just an overall view, at a very high level.
For more information,
Read Scott Mitchell's article on the same topic
Though this question has been answered in hundreds of blogs and articles online, some of us still consider this to be a grey area and leave it mostly to the developer's preference.
Well...here's what i understand about these web controls.
First of all, all the three controls were created with the same objective, which is to display data. So why are there 3 different controls?
When these controls are bound to a datasource, for each entity in the datasource an "item" is created and added to the collection of items for the control.
Each Webcontrol's "item" belongs to a different class. For example, a DatagridItem class is derived from TableRow class. This is because the HTML rendered by a datagrid is nothing but an HTML table with rows and columns based upon the datasource it was bound to. Hence each item in a datagrid collection is nothing but an HTML table row, when it is displayed in the browser.
One big advantage of the datagrid is that it is very simple to use and easily customizable with some cool color settings for borders,backgrounds etc. It also supports paging and sorting which also is accompanied by some customizable features for example, the way the page index can be displayed, as numbers or "previous and next" links etc.
So if you have a source of data that needs to be displayed with no customization or no complex stuff, datagrid is the best choice.
But remember, you will have no control over the HTML table that is being rendered.
Like the rows, each column in a DataGrid is an instance of a class that is derived from the DataGridColumn class. There are five built-in DataGrid column types:
* BoundColumn
* ButtonColumn
* EditColumn
* HyperLinkColumn
* TemplateColumn
Besides these built-in data types we can also create our own customized DataGrid column types by creating a class that derives from the DataGridColumn class.
Most of the newbies out there do not pay attention to the most important issue in datagrids ; Viewstate. ViewState produced by the DataGrid can be humongous if it contains a considerably large number of rows and thus be detrimental to performance. Of course, you DO have the option to turn the viewstate off but then that doesn't help you if you are planning to use sorting, paging and editing features in the datagrid.
Apparently the datagrid has the worst performance of all the 3 web controls.
The Repeater Web control offers the most flexibility as far as control of the rendered HTML is concerned. The datagrid and datalist controls place the content within a HTML tag like <table> or <span> automatically. That's how they are rendered. But a Repeater control renders ONLY what is specified by the developer. For this reason, if you wish to display data in some way other than in an HTML <table> tag or in a series of <span>tags, Repeater control is the best choice.
A Repeater is designed to let the user customize its output and thus have complete control over what is being rendered. So understandably RepeaterItem class is not derived from the TableRow class.
Just like in the DataList, with Repeater you specify the markup using templates. The Repeater contains the following five templates:
* AlternatingItemTemplate
* FooterTemplate
* HeaderTemplate
* ItemTemplate
* SeparatorTemplate
An important thing to remember about the Repeater is that, it is not derived from the WebControl class, like the DataGrid and DataList due to which it lacks the stylistic properties common to both the DataGrid and DataList. So if you want to make things look pretty in a repeater, write it yourself ;). But in most cases this is what i want : Complete control over what is being rendered.(Yupp... i AM a control freak!)
Among all the 3 web controls in question, Repeater offers the best performance, though only slightly ahead of the datalist but beating datagrid by a considerable margin.
Finally about the datalist.
The DataList does not use columns as in datagrid.It uses templates to display items just like Repeaters. The advantage of using a template is that with a template, you can specify both a mix of HTML content and databinding syntax.
Along with the ItemTemplate the DataList supports six other templates for a total of seven:
* AlternatingItemTemplate
* EditItemTemplate
* FooterTemplate
* HeaderTemplate
* ItemTemplate
* SelectedItemTemplate
* SeparatorTemplate
By default, the DataList displays each item as an HTML tablerow. But an important property of datalist is the RepeatColumns property. Using this property you can specify how many DataList items should appear per table row. In addition to that, you can also specify if you want the contents of the DataList should be displayed using <span> tags instead of a <table> tags. This is done by setting the RepeatLayout property, to either Table or Flow, which would render the data in HTML <table> tags or in <span> tags.
With its templates and RepeatColumns and RepeatLayout properties, it's obvious that the DataList allows for much more customization of the rendered HTML markup than the DataGrid. Thus customization again, is the reason why one might prefer to use a datalist over datagrid.
One major advantage of using Datagrid over datalist and repeater is, as already mentioned, the sorting and paging functionality. While such functionality can be implemented with some smart logic in the code, it's the question of how much time and effort would be involved in doing it and whether the trade-off is worth it in the application development process.
Guess ...that's about it. This is just an overall view, at a very high level.
For more information,
Read Scott Mitchell's article on the same topic
24 Comments:
Great article! I personally try to limit my use of the DataGrid as much as possible. It's incredibly slow and hard to work with. The DataList has the distinct advantage of the RepeatDirection property, which makes creating a photo album very quick and easy.
Hi.. Nice one. Cleared a lot of questions out of my mind about using these controls.
The URL for Scott Mitchell's article may need to be corrected.
Thanks.
Really Great article. It gives clear purpose of using of the control..
This article is looks pretty simple and covers all what is needed to know about these controls.
Nice Article. Thanks,
Hi, Good article really helpul in understanding these controls
cheers for the article - seems I must be a control freak too - repeaters all the way :D
Thanks for describing in detail.
here is the correct link to Scott Mitchell's article:
http://msdn2.microsoft.com/en-us/library/aa479015.aspx
its really nice and helpful one
Great article. I wish you could also include about the editing capabilities of these three controls.
Good Article.. Thanks
Good article!!! Thanks for the valuable inputs.
Sathya ^_^
Wonderful article !! It helps me a lot in choosing right data control. Thanks a lot :)
Clear and concise. Very useful.
Thank you Girish for such a simple description of controls which appears somewhat stranger to a newbie.This is one of the most frequently asked topic in am interview for a .Net Developer.After reading this article one should gets the confidence of answering any questions from this topic.Wonderful !!!
Thank You Once Again
This is Joshy M Raj from India
Wonderful!
Simply awesome!
Simply Great!
Keep it up!
Great article indeed
HI, That is a good one indeed; but plz fix the broken link. I'm not sure if u r reading the user comments..
Ya its a great article and i have enjoyed reading it. thanks for the post.
Its an amazing one.I loved it
Satyendra Singh
this article is good and it will be helpful.
Thanks..
It really helped me as even i used to wonder before what to use .
Kudos!.. you put some light on this issue..
Post a Comment
<< Home