dotnetmonitor.com |
|
||||||||||||
| Windows Forms data
binding integrates with the Windows Forms ErrorProvider control. When using
the ErrorProvider and if a simple data binding operation fails, the
ErrorProvider control will provide visual feedback (an error icon) on the user
interface element that experienced the failure. The ErrorProvider looks for
exceptions that occur during binding and will additionally report
IDataErrorInfo information on data sources that support this interface.
Sample: Error Handling (VS 2005) (VS Project: DataBinding Intro) /******************************************************************* * Setup (using the Visual Studio Form designer): * * Add 2 TextBoxes to the Form (textBox1 and textBox2) * Add an ErrorProvider to the Form (errorProvider1) * 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 ******************************************************************/
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);
/******************************************************************* * Set up the ErrorProvider: * * Bind it to the same data source used by the TextBoxes. ******************************************************************/
this.errorProvider1.DataSource = _dt;
/******************************************************************* * 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);
/******************************************************************* * Bind TextBox.Text (string) to Numbers.Cost * The binding runtime will display the value as Currency ($value.00) * * Note that this manually creates and adds the Binding to the * controls DataBinding collection rather than using the * DataBindings.Add overload. ******************************************************************/
Binding cb = new Binding("Text", _dt, "Cost", true);
/* .NET Framework Currency format string */ cb.FormatString = "c";
/* Add the binding to the Textbox */ this.textBox2.DataBindings.Add(cb); |
||||||||||||
|
||||||||||||