DataTable vs. BindingList<T>

We were having a discusion today about the merits of using DataTables vs. BindingList<T> (a generic in .NET 2.0) for loading up domain objects into the UI layer (say to display on a grid). My gut feel is telling me DataTables are evil and wrong, but I don't have a lot of hard evidence to choose one over the other. We brainstormed some pros and cons for each and they're listed below.

  • DataTable
    • Pros
      • Simple to implement, not much code needed
      • Can use select statements to retrieve values
      • Change events are managed within the object automagically
      • Display handling for errors built-in (when binding to grids)
    • Cons
      • Human errors (typos on column names for example) can be exposed at runtime and can't be easily tested
      • Need to implement DataViews in order to sort or filter
      • Can't test for type safety (or as Scott says DataTables are bowls of fruit)
      • Difficult to implement business rules or logic
      • Bloatware, lot of extra baggage for features you don't need
  • BindingList<T>
    • Pros
      • No mapping needed
      • Strongly typed and type-safe
      • Loosly coupled compared to a data table -> mapping columns -> domain object
      • More extensible via superclasses, interfaces, etc.
    • Cons
      • Must iterate through list to find items
      • Must implement some mechanism (like a decorator pattern) to respond to change events
      • More short term investment to learn and implement

Are we wrong with our list or is there something that you've come across with your dealings with the two approaches?

Published Wednesday, April 4, 2007 12:59 PM by Bil Simser
Filed under:

Comments

# re: DataTable vs. BindingList<T>

Wednesday, April 4, 2007 4:28 PM by FransBouma

I'd go for bindinglist<T>. The thing is that with a datatable, the dataview which is bound to the control also has to do the same things as bindinglist<T> has to do: both implement ITypedlist and provide property info for the objects in the list/dataview (done once).

A dataview also iterates through items to find rows, though it could create indexes, however I don't think that will matter that much.

About bloatware: code of a class is shared among all instances of the class. This means that if you instantiate a lot of objects of the same class, you won't lose extra memory if the class is big: all objects share the same code / IL in memory

# re: DataTable vs. BindingList<T>

Wednesday, April 4, 2007 6:42 PM by allen

Go with BindingList<T>.  Its pros outweight the DataTable pros.  The other concern with DataTables is the possiblity of Microsoft implementing another data access framework (DAO,RDS,ADO,ADO.NET, etc.).  You can't go wrong with using business objects.  It is more work but you will have a better solution in the long run.

Also, Brian Noyes has a great book on .Net 2.0 Databinding.  It has examples on how to wire your business objects up to easily maintain their data changes.

# re: DataTable vs. BindingList<T>

Wednesday, April 4, 2007 6:43 PM by Travis

Why anyone would consider using a DataTable is beyond me.

# re: DataTable vs. BindingList<T>

Wednesday, April 4, 2007 9:00 PM by Jesse Johnston

I'm a fan of BindingList<T> as well.  The one thing you lose with BindingList though is the sorting and filtering that are available over DataTables through the DataView.  You can filter and sort the DataView without affecting the underlying table.  Very nice.

Unfortunately, there is no such "out of the box" view for arbitrary domain objects in a BindingList<T>.  To solve this, I wrote ObjectListView, which provides a sorted, filtered view over any List<T> (all without altering the underlying list).  Check it out at my blog: www.teamjohnston.net/cs/blogs/jesse.

Cheers,

Jesse Johnston

# re: DataTable vs. BindingList<T>

Wednesday, April 4, 2007 10:19 PM by The Coding Hillbilly

There is nothing inherently wrong with DataTables. Just use them when the pros outweigh the cons. Not every application is going to get Amazon's traffic. If I'm writing a one-off utility for my own personal use to read in an Excel spreadsheet, for example, I'll read it directly into a DataSet with an OleDbAdapter and bind to the table without bothering to convert to objects.

In short, I use DataTables for quick and dirty but I don't get attached to them. They're the first thing in the list of refactorings if the app starts growing even a little.

# re: DataTable vs. BindingList<T>

Thursday, April 5, 2007 4:13 AM by FransBouma

Travis:

try to bind a collection of strings to a grid and add a new one :)

# re: DataTable vs. BindingList<T>

Thursday, April 5, 2007 5:56 AM by Cornelius van Berkel

Hi,

Using a strongly typed DataTable would negate the con regarding human errors. However its inabillity to map DBNull to Nullable<T> is a strike out for me.

I recently looked at the innards of the Select mechanism and concluded that performance-wise & maintainabillity you're much better off using either Linq (the compile time checking) or just rolling your own index / iterator...

Cheers

# re: DataTable vs. BindingList<T>

Thursday, April 5, 2007 5:59 AM by Cornelius van Berkel

p.s.

I meant using Linq or your own index/iterator on the BindingList<T>.

On a side note; I do like the change tracking of the datatable, allthough the implementation chosen is hardly sufficient for my needs.

# re: DataTable vs. BindingList<T>

Thursday, April 5, 2007 1:32 PM by kevindente

The biggest annoyance with BindingList<T> is that it doesn't natively support sorting. I can't believe MS left that out. It's not too horribly difficult to subclass and add, but it's annoying that we have to.

# re: DataTable vs. BindingList<T>

Saturday, June 5, 2010 9:43 PM by xmen

I never needed BindingList, DT still on top.

# re: DataTable vs. BindingList<T>

Tuesday, February 15, 2011 12:00 PM by SHAKTI RAJPAL

use LINQ to search elements in List<>

# re: DataTable vs. BindingList<T>

Wednesday, March 2, 2011 3:31 AM by Steven Oberholzer

I've only heard of the BindingList recently, and so far I've been able to make do with the DataTable object. It is actually a powerful object that has many more pro's than mentioned, least of which is persistance to XML. I'm going to dapple a bit with the BindingList as I'm sure it is lighter. I wouldn't shoot down either the BindingList or the DataTable as each has their place.

# re: DataTable vs. BindingList<T>

Monday, October 3, 2011 2:07 PM by Steve Reilly

No System.Data (hence no DataTables) for Metro UI / WinRT apps ...

# re: DataTable vs. BindingList<T>

Friday, November 25, 2011 8:09 AM by Abbas

(a) I started building a mission critical application by building custom entity classes based on a common base class for business rules and state tracking. Sooner I found that BindingList<T> does not have filter support.

(b) Then I started building the project with typed dataset approach. It was found that you can't set numeric keys (foreign keys) to DBNull. Another issue is that Typed Datatable generated 10000 lines of code just for 2-3 tables. That is not acceptable.

(c) Untyped datatable: you can't cancel the columnchanging operation from the columnchanging event. If enduser enters a wrong value, you can't simply reject it in the column changing event. The result is that the status of the row is set to 'modified'.

The end result is that you end up messing code. Writing workarounds and validation logic on the frontend. And your code starts growing and growing.