Thursday, April 26, 2007

Tutorial about Binding with ComboBox



Let say that you have a list of Person that you would like to display into a combobox. To fill a combo box with C#2.0 is pretty simple. First of all, you required a List of strong typed object, for our example a list of Person. Second, you need to bind the list to the component. Finally, you require to tell what's to display and what to hold for value.




Code:
List lstPerson = new List();
lstPerson.Add(new Person("Mary"));
lstPerson.Add(new Person("Sam"));
lstPerson.Add(new Person("Joe"));
this.cboPersons.DataSource = lstPerson;
this.cboPersons.DisplayMember = "Name";
this.cboPersons.ValueMember = "Name";




The final result is simply a combobox with all the three names.


Data Binding - Small tutorial about binding class member to a textbox

C# with its framework 2.0 introduces a new way of thinking with DataBinding. A lot of improvements have been done since the framework 1.0.

First of all, less code is required between component and business object. It's always possible to get deeper in the code; thence complex thing could still be done.

First of all, the way to bind simple object (instencied class) is easily done within less than 2 lines. Lets stay you want to bind the property Name of your Person class to a textbox.

Code would be: Person p = new Person("Henry");
this.txtName.DataBindings.Add(new Binding("Text", p, "Name"));

This code show a textbox that display "Henry" on it.

Welcome

This is the first post in this blog. This blog will contain many experience with design pattern and about C# with WinForm at first and maybe later with ASP.NET.

This Blog as for goal to show some interesting stuff about C#, Visual Studio and other nice feature that .Net provide.