1 / 6

Programming in C# Data Binding to Controls

Programming in C# Data Binding to Controls. CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis. The BindList class. The BindingList class provides a generic wrapper around a collection supporting the IList interface.

Download Presentation

Programming in C# Data Binding to Controls

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming in C#Data Binding to Controls CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis

  2. The BindList class • The BindingList class provides a generic wrapper around a collection supporting the IList interface. • It provides list changed events and many other properties (including sorting). • The easiest way to create one is by passing in an existing collection: IList<int> numberList = newList<int>(20); BindingList<int> bindingNumbers = newBindingList<int>(numberList);

  3. Example • A simple Windows Form with a listView a ComboBox and a button to add more items.

  4. Example publicpartialclassForm1 : Form { privateBindingList<int> bindingNumbers; privateIList<int> numberList = newList<int>(20); privateintnextNumber = 10; publicForm1() { InitializeComponent(); for(inti = 0; i < nextNumber; i++) numberList.Add(i); bindingNumbers= newBindingList<int>(numberList); listBox1.DataSource = bindingNumbers; comboBox1.DataSource = bindingNumbers; }  privatevoidaddButton_Click(object sender, EventArgse) { bindingNumbers.Add(nextNumber++); } }

  5. Formatted Text • Note that the controls displayed the integers. • Each control will use a Formatter to convert the object to a string representation. • This is typically the default ToString() method of the object. • Works for any type:

  6. Example 2 publicpartialclassForm1 : Form     { privateBindingList<object> bindingObjects; privateIList<object> objectList = newList<object>(20); public Form1()         { InitializeComponent(); for (inti = 1; i < 10; i++) objectList.Add(newDateTime(2008,5,i)); for (inti = 10; i < 14; i++) objectList.Add(i); objectList.Add("Hello World"); objectList.Add(numberList); bindingObjects = newBindingList<object>(objectList);             listBox1.DataSource = bindingObjects;             comboBox1.DataSource = bindingObjects;

More Related