So many different controls to choose from! What are best practices for determining which control to use for displaying data in ASP.NET?
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
It’s really about what you trying to achieve
- Gridview – Limited in design, works like an html table. More in built functionality like edit/update, page, sort. Lots of overhead.
- DataGrid – Old version of the Gridview. A gridview is a super datagrid.
- Datalist – more customisable version of the Gridview. Also has some overhead. More manual work as you have to design it yourself.
- ListView – the new Datalist :). Almost a hybrid of the datalist and gridview where you can use paging and build in Gridview like functionality, but have the freedom of design. One of the new controls in this family
- Repeater – Very light weight. No built in functionality like Headers, Footers. Has the least overhead.
Everyone else hit it: It Depends.
Now for some specific guidance (expanding upon WebDude’s excellent answer above) …
Does your design fit into a natural spreadsheet or grid view of the data? GridView.
Do you need to display a list or other formatted view of data, possibly with headers and footers, and probably with specific controls and/or formatting for each record of data? (EG, customized links, possibly LinkButtons, or specific edit controls?) Does this display specifically not fit naturally into a spreadsheet or grid view? ListView
If you meet all the criteria of ListView, but you would naturally fit in a grid, you may consider DataList.
I go for Repeater when I just need some basic data iterated with some custom design bits, no headers, no footers, nice and clean.
Declaring the following sample code is possible for all 3( ListView, DataList , Repeater)
<asp:ListView runat="server" OnItemCommand="Unnamed1_ItemCommand"> <ItemTemplate> <%# Eval("Name")%> </ItemTemplate> <asp:ListView>
in the following lists You can see the available templates and options for each of them and see the differences for yourself
ListView (note the edit,group,insert ,layout)
DataList (note the Style pairs)
Code View (advanced view)
look the following classes hierarchy (and related controls).
these controls hosts other asp.net controls in their templates to display bound-data to user
Some descriptions for better clarifications
The ListView Control
The ListView control also uses templates for the display of data. However, it supports many
additional templates that allow for more scenarios when working with your data. These templates include the LayoutTemplate,GroupTemplate,ItemSeparatorTemplate.
The ListView control (unlike DataList and Repeater) also implicitly supports the ability to
edit, insert, and delete data by using a data source control. You can define individual templates
for each of these scenarios.
The DataList Control
The DataList control works like the Repeater control. It repeats data for each row in your data set,
and it displays this data according to your defined template. However, it lays out the data defined
in the template within various HTML structures. This includes options for horizontal or vertical
layout, and it also allows you to set how the data should be repeated, as flow or table layout.
The DataList control does not automatically use a data source control to edit data. Instead,
it provides command events in which you can write your own code for these scenarios. To
enable these events, you add a Button control to one of the templates and set the button’s
CommandName property to the edit, delete, update, or cancel keyword. The appropriate
event is then raised by the DataList control.
The Repeater Control
The Repeater control also uses templates to define custom binding. However, it does not show data as individual records. Instead, it repeats the data rows as you specify in your template. This
allows you to create a single row of data and have it repeat across your page.
The Repeater control is a read-only template. That is, it supports only the ItemTemplate.
It does not implicitly support editing, insertion, and deletion. You should consider one of the
other controls if you need this functionality, otherwise you will have to code this yourself for
the Repeater control.
The above Descriptions are from MCTS Exam 70-515 Web Applications Development with Microsoft.NET Framework 4 book.
DataGrid is not even mentioned in this book and is replaced by popular GridViews and answered nicely by other users
Indeed! I’ve blogged on the differences between the ASP.NET 4.0 data tools. Basically, gridviews are the most powerful way to present tabular information, whereas ListView controls are for more complicated displays of repeated data. If I were giving advice to an ASP.NET newbie, I’d tell them to learn gridviews inside out and ignore the other controls to begin with.
It all comes down to how you want to layout your data.
If you need to control the layout (like tables versus CSS versus whatever), when use a Repeater or ListView. Between the two, ListView gives you a lot more events and built-in commands for editing, selecting, inserting. Additionally paging and grouping functionality. A Repeater is extremely simple, it repeats a layout with the data. Since you’re building the layout by hand, Listview and Repeater require more code.
GridView is an updated DataGrid so there is hardly any reason to use DataGrid. GridView works really well when hooked up to standard ASP.NET datasources, but restricts you to a tabular layout with lots of layout rules. GridView requires less code since you’re using a built-in layout.