dotnetmonitor.com

 
Index
Previous
Next

 

If required, Windows Forms will perform type conversion as part of the binding process. For example, if an integer property on a business object (e.g. Customer.ID) is bound to a string property on a Control (e.g. TextBox.Text) the data binding runtime will convert the integer value to/from the string value. Windows Forms uses TypeConverters, IFormattable and IConvertible to perform the type conversion.

Sample: Type Conversion (VS 2005) (VS Project: DataBinding Intro)

/*******************************************************************

* Setup (using the Visual Studio Form designer):

*

* Add a TextBox to the Form (textBox1)

* Add the code below to the Form.Load event

******************************************************************/

 

/*******************************************************************

* Data Source setup:

*

* Create a Table named Numbers and add 3 columns

* ID: int

* Name: string

* Cost: Decimal

******************************************************************/

 

/*******************************************************************

* A DataTable is an ADO.NET list type that represents a relational

* table.

******************************************************************/

 

DataTable _dt;

 

_dt = new DataTable("Numbers");

 

_dt.Columns.Add("ID", typeof(int));

_dt.Columns.Add("Name", typeof(string));

_dt.Columns.Add("Cost", typeof(decimal));

 

_dt.Rows.Add(0, "Zero", 10.0M);

_dt.Rows.Add(1, "One", 11.1M);

_dt.Rows.Add(2, "Two", 12.2M);

 

/*******************************************************************

* Bind TextBox.Text (string) to Numbers.ID (integer)

* The binding runtime will handle type conversion between integer

* and string

******************************************************************/

this.textBox1.DataBindings.Add("Text", _dt, "ID", true);

Simple Binding
Complex Binding
Using Simple Binding with a List
Sample: Simple Binding to a List (VS 2005) (VS Project: DataBinding Intro)
Formatting