Monday, January 31, 2005

DataBindings in C# .net

I found this interesting in C# the other day I was working on some GUI code. One can actually bind a widget (eg:- ComboBox to an IList ). The IList can be a collection of objects of any custom class, maybe even a DTO.

So if I have my custom class as

class DocumentTypeDTO
{
....
public string Name
{
get
{
return _name;
}
}
public string Type
{
//Getter
}
public string Code
{
//Getter
}
}

I can actually bind my control to an array of DTOs .

_documentTypeComboBox.DataSource = documentTypeDTOs;

To specify what member has to be used for display, I have a DisplayMember setter on my widget which takes a property name string.

_documentTypeComboBox.DisplayMember = "Name";

I can also define the value which will get picked up on the selection of a combobox item by:

_documentTypeComboBox.ValueMember = "Code";

Apparently .net uses reflection to get the members out . Sweet !!



1 comment:

Anonymous said...

Yeah, it's sweet until you give it a list containing different implementations of the same interface. The concrete type of each list item must be identical.