by Damiaan Peeters
7. December 2007 17:10
Learn about the generic BindingList and how to extend this generic collection type to add sorting and searching functionality.
Behind the Scenes: Improvements to Windows Forms Data Binding in the .NET Framework 2.0, Part 2
by Damiaan Peeters
2. December 2007 14:13
When you search a lot on MSDN, you might encounter from time to time a nice example. Yesterday I found such a nice example on the BindingSource Class page.
They created a new new Generic BindingList of the Font Class
public class MyFontList : BindingList<Font>
{
protected override bool SupportsSearchingCore
{
get { return true; }
}
protected override int FindCore(PropertyDescriptor prop, object key)
{
// Ignore the prop value and search by family name. for (int i = 0; i < Count; ++i)
{
if (Items[i].FontFamily.Name.ToLower() == ((string)key).ToLower())
return i;
}
return -1;
}
}
To use this class, just fill the MyFontList and attach it to a bindingSource.
MyFontList fonts = new MyFontList();
for (int i = 0; i < FontFamily.Families.Length; i++)
{
if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Regular))
fonts.Add(new Font(FontFamily.Families[i], 11.0F, FontStyle.Regular));
}
binding1.DataSource = fonts;
listBox1.DataSource = binding1;
listBox1.DisplayMember = "Name";