The Difference Between a DataGrid and a GridView in ASP.NET?

asp.net

asp.net Problem Overview


I've been doing ASP.NET development for a little while now, and I've used both the GridView and the DataGrid controls before for various things, but I never could find a really good reason to use one or the other. I'd like to know:

What is the difference between these 2 ASP.NET controls? What are the advantages or disadvantages of both? Is one any faster? Newer? Easier to maintain?

The intellisense summary for the controls doesn't seem to describe any difference between the two. They both can view, edit, and sort data and automatically generate columns at runtime.

Edit: Visual Studio 2008 no longer lists DataGrid as an available control in the toolbox. It is still available (for legacy support I assume) if you type it in by hand though.

asp.net Solutions


Solution 1 - asp.net

DataGrid was an ASP.NET 1.1 control, still supported. GridView arrived in 2.0, made certain tasks simpler added different databinding features:

This link has a comparison of DataGrid and GridView features -

https://msdn.microsoft.com/en-us/library/05yye6k9(v=vs.100).aspx

Solution 2 - asp.net

The GridView control is the successor to the DataGrid control. Like the DataGrid control, the GridView control was designed to display data in an HTML table. When bound to a data source, the DataGrid and GridView controls each display a row from a DataSource as a row in an output table.

Both the DataGrid and GridView controls are derived from the WebControl class. Although it has a similar object model to that of the DataGrid control, the GridView control also has a number of new features and advantages over the DataGrid control, which include:

  • Richer design-time capabilities.
  • Improved data source binding capabilities.
  • Automatic handling of sorting, paging, updates, and deletes.
  • Additional column types and design-time column operations.
  • A Customized pager user interface (UI) with the PagerTemplate property.

Differences between the GridView control and the DataGrid control include:

  • Different custom-paging support.
  • Different event models.

Sorting, paging, and in-place editing of data requires additional coding when using the DataGrid control. The GridView control enables you to add sorting, paging, and editing capabilities without writing any code. Instead, you can automate these tasks, along with other common tasks such as data binding to a data source, by setting properties on the control.

Solution 3 - asp.net

The DataGrid was originally in .NET 1.0. The GridView was introduced (and replaced the DataGrid) in .NET 2.0. They provide nearly identical functionality.

Solution 4 - asp.net

If you're working in Visual Studio 2008 / .NET 3.5, you probably shouldn't use either. Use the ListView - it gives you the features of the GridView combined with the styling flexibility of a repeater.

Solution 5 - asp.net

The key difference is in the ViewState management IIRC. The DataGrid requires ViewState turned on in order to have edit and sort capabilities.

Solution 6 - asp.net

One key difference security wise is that DataGrid uses BoundColumn which does not HtmlEncode the bound data. There is no property to turn HtmlEncoding on or off either, so you need to do it in code somehow.

GridView uses BoundField, which does HtmlEncode by default on the bound data and it has a HtmlEncode property if you need to turn it off.

Solution 7 - asp.net

DataGrid

  1. DataGrid was introduced with Asp.Net 1.0.
  2. For sorting we need to handle SortCommand event and rebind grid required and for paging we need to handle the PageIndexChanged event and rebind grid required.
  3. Need to write code for implementing Update and Delete operations.
  4. Does not supports auto format or style features.
  5. Performance is fast as compared to GridView.

GridView

  1. GridView was introduced with Asp.Net 2.0.
  2. Built-in supports for Paging and Sorting.
  3. Built-in supports for Update and Delete operations.
  4. Supports auto format or style features.
  5. Performance is slow as compared to DataGrid.

The events and properties like Item has changed as Row.

For example,

  • ItemCommand - RowCommand

  • ItemDataBound - RowDataBound

  • e.Item.ItemType - e.Row.RowType

Solution 8 - asp.net

some basic diffrence between gridview and details view

the GridView control also has a number of new features and advantages over the DataGrid control, which include:

· Richer design-time capabilities. · Improved data source binding capabilities. · Automatic handling of sorting, paging, updates, and deletes. · Additional column types and design-time column operations. · A Customized pager user interface (UI) with the PagerTemplate property.

Differences between the GridView control and the DataGrid control include: · Different custom-paging support. · Different event models.

Solution 9 - asp.net

One of the differences is the HTML output. A datagrid will output TD's for the header and a gridview will output TH's. This can cause unintuitive changes in the display.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDan HerbertView Question on Stackoverflow
Solution 1 - asp.netKevView Answer on Stackoverflow
Solution 2 - asp.netSuhaib JanjuaView Answer on Stackoverflow
Solution 3 - asp.netBrandon WoodView Answer on Stackoverflow
Solution 4 - asp.netJon GallowayView Answer on Stackoverflow
Solution 5 - asp.netAndrei RîneaView Answer on Stackoverflow
Solution 6 - asp.netslolifeView Answer on Stackoverflow
Solution 7 - asp.netLitisqe KumarView Answer on Stackoverflow
Solution 8 - asp.netkuldeep singh chouhan View Answer on Stackoverflow
Solution 9 - asp.netjmorenoView Answer on Stackoverflow