This blog has been moved to Redwerb.com.

Sunday, March 18, 2007

Data Binding Classes, Interfaces, and Attributes in Windows Forms 2.0

Data binding has been greatly improved in .Net 2.0. It is now very easy to bind to any type of object or collection of objects and get a great design-time experience plus a pretty good run-time experience. However, to fully realize the potential for binding in .Net 2.0 you need to be aware of all of the classes, interfaces, and attributes that collaborate to provide the rich interactions available.

This post is not intended to be a tutorial. It is simply a list of the classes and interfaces that you should be aware of when building a data bound application. If you are looking for a good book on this subject, I would recommend Data Binding with Windows Forms 2.0 by Brian Noyes.

Binding Classes

These classes allow you to bind your data source to the controls on a form.

BindingSource

The BindingSource class handles all of the interactions between the controls and the actual instance of the data source. It is the primary class used in data binding. A BindingSource is automatically created when you bind a control to a data source.

This class can adapt to many different types of data sources from the very simple (example, a basic class that exposes public properties) to the very complex (example, a collection of objects that have custom properties that can change dynamically during the life of the object). The interfaces that this class can collaborate with are discussed in the Data Source Interfaces section.

Inherits: Component
Implements: IBindingList, IBindingListView, ITypedList, ICancelAddNew, ISupportInitializeNotification, ISupportInitialize, ICurrencyManagerProvider
Collaborates With: Binding, CurrencyManager, IBindingList, IBindingListView, ISupportInitializeNotification, ITypedList, IList, IListSource, PropertyDescriptor, IEnumerable, IRaiseItemChangedEvents, ICancelAddNew, ISupportInitializeNotification, ICurrencyManagerProvider


Binding

The Binding class associates a property on a control to a property on a data source and also associates it with a BindingSource instance to handle the actual interaction between the control and the data source.

This class exposes events for converting values from one data type to another. So, if you have special formatting/parsing logic, you might need to hook up to these events. If you have a custom class that can be converted to/from a string, you can also implement the IFormattable interface along with a public static <MyObject> Parse(<string>) method, implement the IConvertible interface, or associate the class with a TypeConverter (there are many formatting/parsing options). The TypeConverter might be the best option because of the additional support it provides for conversions, however, it is also the most complex. Implementing the IConvertible interface is the second best option and it allows your class to be converted using the System.Convert class.

Collaborates With: BindingSource, BindingManagerBase, IBindableComponent, IFormatProvider, IFormattable, TypeConverter, IConvertible

Data Source Interfaces

Implement these interfaces on your custom data source to provide the features required for binding. As a note, you can replicate the behavior of binding to a DataSet by implementing these interfaces (this is how the DataSet, DataTable, DataView, DataRow, and DataRowView classes bind to controls).

IBindingList

The IBindingList interface provides many of the properties and methods that the BindingSource needs to provide the rich interaction that most people expect from data binding. This interface allows data items to be added and removed as well as providing notification when a data item is removed, added, or modified).

The BindingList<T> class implements this interface and allows this interface to be used without having to write a lot of code.

Inherits: IList

IListSource
The IListSource interface allows a bound data item to provide a list without implementing the IList interface directly. It can also provide a list of lists if the ContainsListCollection property returns true.

ITypedList

The ITypedList interface allows a list to provide information about the properties that are available for the data items that it contains. This interface is used by the Visual Studio designer to determine the properties that are available for binding.

The GetItemProperties method on this interface can be used to get the properties for related data items or lists by passing in the property descriptors that define the relationship. The GetListName method is not really used by the .Net FX. It was used by the old data grid in version 1.1 to display the names of the child lists while navigating the grid


IBindingListView

The IBindingListView interface adds support for advanced filtering and sorting to the IBindingList interface.

Inherits: IBindingList

ICancelAddNew

The ICancelAddNew interface is implemented by a collection class and allows new data items to exist in a transient state (it’s not yet added to the collection, but will be once the user has completed editing it). This can be used in conjunction with the IEditableObject interface which is implemented by the data item.

Collaborates With: IEditableObject

INotifyPropertyChanged

The INotifyPropertyChanged interface allows a data item to notify a container (such as a list) that a property has changed.

The BindingList<T> class will automatically raise the ListChanged event if a data item in the list implements the INotifyPropertyChanged interface and raises the PropertyChanged event.

Collaborates With: BindingList<T>

IEditableObject

The IEditableObject interface allows a data item to participate in a transaction. You begin the transaction by calling BeginEdit and end it by calling EndEdit or CancelEdit. CancelEdit reverts the record to the state when BeginEdit was called.

Collaborates With: ICancelAddNew

IDataErrorInfo

The IDataErrorInfo interface allows a data item to provide error details to the user. If the data is bound to a DataGridView or there is a ErrorProvider component for the form, an icon is displayed next to the control that is bound to the field and the error is displayed as a tooltip when the user places the mouse over the icon.

Collaborates With: ErrorProvider

ICustomTypeDescriptor

The ICustomTypeDescriptor interface allows a data item to provide a custom set of properties. This is very useful if your data item does not use properties of the class to expose the data (for example, DataRow does not have properties for the data, you have to send in the name of a column to get the data).

The BindingList<T> class calls into this interface when it's properties are called.

Collaborates With: BindingList<T>, BindingSource

Bound Component Interfaces and Attributes

These interfaces and attributes allow you to provide a custom bound component or control.

IBindableComponent

The IBindableComponent interface provides the basic properties needed to hook up binding to a component or control.

Control implements this interface.

Inherits: IComponent
Collaborates With: Visual Studio Designer


ISupportInitialize

The ISupportInitialize interface allows interdependent properties to be set on a component in arbitrary order. This is used primarily for WinForm components added to the designer.

Collaborates With: Visual Studio Designer


ISupportInitializeNotification

The ISupportInitializeNotification interface allows components to be notified of initialization of other components. This is useful if the initialization of one component requires another component to be initialized first.

Inherits: ISupportInitialize


DefaultBindingPropertyAttribute

The DefaultBindingPropertyAttribute identifies the property on the component that should be used as the default binding property. Should match the data type for the property it is being bound to through the data member.

Inherits: Attribute

ComplexBindingPropertiesAttribute

The ComplexBindingPropertiesAttribute tells the VS designer which properties contain the data source and data member for complex data binding.

Inherits: Attribute

Saturday, March 17, 2007

Is Windows Live Writer Dead?

I'm starting to get tired of all the bugs in the Windows Live Writer beta. The last update was released in November of 2006 and the last post on the official team blog is November 21st, 2006. It would be nice if they at least provided some indication that Microsoft is still working on this project.

Thursday, March 15, 2007

Microsoft Briefcase

One thing I didn't mention in my last post (Portable Applications) was Microsoft Briefcase. Briefcase has been around for a while, but I never got around to figuring out what it was before the other night. Basically, Briefcase allows you to carry around copies of documents and synchronize them when they are updated. It's very easy to use and doesn't require anything other than Windows Explorer to use (it's built in).

My wife carries some work documents on a USB thumb drive so that she can easily work on them from multiple locations (home and two different offices). She didn't really have a backup strategy for the documents so if she lost or broke the USB thumb drive, many hours of work would be lost. She didn't want to use a full backup solution because of the complexity (plus the range of options to choose from). When I found Briefcase, I was pretty sure this would be a good solution for her.

To create a Briefcase, simply open Windows Explorer to the directory you want to create it in (probably your thumb drive), right-click in the empty area to bring up the context menu, expand the New menu, and select Briefcase. Once the Briefcase is created, you can drag the documents you want to keep synchronized into it. You can view the documents in the Briefcase from Windows Explorer and open them just like you would any other file. When you want to synchronize the Briefcase, simple right-click on it and select Update All. It brings up a window that shows you the files that are being synchronized and which file is being overwritten.

Here's the KB article from Microsoft if you want to learn more.
How To Use the Briefcase Feature in Windows XP

Tuesday, March 13, 2007

Portable Applications

If you haven't heard yet, portable applications are the new cool thing! Portable applications run from a portable device (usually a USB thumb drive). Typically you install a application launcher on the device and this launcher automatically runs when you connect the device to a computer.

There are many different applications that can be run from a portable device. Some of the more interesting ones include Firefox, OpenOffice (similar to Microsoft Office), Thunderbird (a news/email reader), and many more. One of my favorites is RoboForm2Go (password manager, discussed below).

There are also a number of different application launchers. Portable Apps seems to be a popular (and free) launcher. U3 came installed on the thumb drive I just purchased and it seems to work fairly well. I'm not sure about Portable Apps, but U3 requires a special installation format. However, if you know an application can be portable, you can easily create a U3 installation package by downloading a program called PackageFactory for U3 (ironically, this is not a portable application).

I've been wanting to use portable applications for awhile now and last night I took the plunge. For under $50 I got a 2GB thumb drive that came with an application launcher (U3) and several applications. My primary goal was basically to have a portable password manager and news reader.

The password manager was the most important thing. I have been using a password manager called Billeo for several months. It was nice that I didn't have to remember all of my passwords for all the different sites I visit, but Billeo is basically an Internet bill paying service and they use the password manager to try to get customers. I don't mind the marketing gimmick, but it did mean that the password manager included features I didn't intend to use and thought that perhaps a software company that focused on a password manager might have a better password manager (though Billeo did work very well). Plus, Billeo is not a portable app, so I had to install it on my machine at home and work and then train it (login to a website and save the password) in both places.

The thumb drive came with a password manager, but it didn't work very well. I ended up installing RoboForm2Go instead. RoboForm2Go has all the features I expect in a password manager. Basically, it automatically fills in passwords for me :). It also can be used to populate web forms with common data (such as shipping/billing info). The data can be secured with a master password so that in order to populate the form, you must enter a master password first. With passwords you can protect some and allow others to just automatically populate (for instance, you might want to protect your bank password, but you might not care about your favorite news sites password). RoboForm2Go also includes the ability to have different identities. This is used primarily for filling out forms. You might have one identity with your personal information in it and another for your work information (address, phone, credit card number, etc).

Thunderbird was the other important portable application for me. I subscribe to a number of RSS feeds. Thunderbird has been my RSS reader of choice because I like the preview window. However, the problem with the standard version is that if I wanted to read RSS feeds at home, I wouldn't be able to know which feeds I've already read. Portable Thunderbird solves this problem. I can open Thunderbird from my thumb drive at work or home without having to deal with this issue.

So what portable applications do I have installed?

  • CruzerSync U3 Edition - Backup software that came with the device. I'm not sure if I'll use this or not. It's nice backup software, but I've got a USB backup drive for that (which I believe this is the same software I use for it, though named differently).
  • Process Explorer - This is an advanced version of Windows Task Manager (Ctrl+Shift+Esc). I use it mainly to make comments on what is running on my machine. This allows me to know what all of the software on my machine is for (I hate unknown processes). What I am hoping for (but haven't verified yet) is that the comments are stored in a way that are portable with the application. Probably not, but we'll see.
  • Snippet Compiler - This is a very cool .Net tool that allows you to quickly and easily test out code without having to create a project or anything like that. I just found it and haven't had much of a chance to play with it, but it seems interesting.
  • Color Cop - Allows you to pick colors off of the screen. Very handy when trying to match a color. It gives the value in many different formats so it is easy to import into just about any application that is requesting a color code.
  • RoboForm2Go - This is a password manager (discussed in detail above).
  • Lutz Roeder's .Net Reflector - This is a must-have tool for any .Net developer. It allows you to decompile .Net assemblies and read the code in whatever .Net language you choose (C#, VB.Net, etc). I have used this frequently to learn how different parts of the .Net framework work.
  • Notepad2 - Very similar to Notepad (the text editor built into Windows), but with a few extra features and more reliable. What's nice about Notepad2 is that it is not trying to be a code editor or anything else.
  • avast! antivirus - This is a free antivirus utility. It has gotten a lot of positive feedback, but I already have an antivirus, so it's unlikely I will use it.

Wednesday, March 07, 2007

Cat Herding 101

Fast Company magazine has published an interesting article on managing software engineers titled How to Manage Geeks. Of course, I like it because it says that I should be treated better :). They interviewed the CEO of Novell, Eric Schmidt, who has a strong technology background as well as being credited with turning around Novell, which many believed was doomed.

It does seem like software engineers can be a very challenging group of people to manage, especially for managers that don't understand their unique characteristics. The article seems to imply that the best managers are those that simply facilitate the natural tendencies of software engineers. It also mentions that engineers rarely make good managers. I imagine that's probably true. Of course, Schmidt himself shows that there are certainly exceptions to this rule (including some very notable ones like Bill Gates and Steve Jobs).