May 2006 - Posts
public
class MergeMultiColumsBinding :
Binding
{
public string[] Properties;
public MergeBinding(
string property ,
object ds ,
string member ,
params string[] prop )
:
base( property , ds , member )
{
this.Properties = prop;
}
protected override void OnFormat( ConvertEventArgs e )
{
CurrencyManager cm = this.Control.BindingContext[this.DataSource] as CurrencyManager;
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties( cm.Current );
StringBuilder sb = new StringBuilder();
for( int i = 0 ; i < Properties.Length - 1 ; i++ )
{
sb.AppendFormat( "{0} ," , pdc[Properties[i]].GetValue( cm.Current ) );
}
sb.AppendFormat( "{0}" , pdc[Properties[Properties.Length - 1]].GetValue( cm.Current ) );
e.Value = sb.ToString();
}
protected override void OnParse( ConvertEventArgs e )
{
string[] values = e.Value.ToString().Split( ',' );
CurrencyManager cm = this.Control.BindingContext[this.DataSource] as CurrencyManager;
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties( cm.Current );
for( int i = 0 ; i < Properties.Length ; i++ )
{
pdc[Properties[i]].SetValue( cm.Current , values[i] );
}
}
}
Step 1: Building MergeBinding:
public
class MergeBinding :
Binding
{
public string PropA;
public string PropB;
public MergeBinding( string prop , object ds , string member, string propa, string propb )
: base( prop , ds , member )
{
this.PropA = propa;
this.PropB = propb;
}
protected override void OnParse( ConvertEventArgs e )
{
string[] values = e.Value.ToString().Split( ',' );
CurrencyManager cm = this.Control.BindingContext[this.DataSource] as CurrencyManager;
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties( cm.Current );
pdc[PropA].SetValue( cm.Current , values[0] );
pdc[PropB].SetValue( cm.Current , values[1] );
}
protected override void OnFormat( ConvertEventArgs e )
{
CurrencyManager cm = this.Control.BindingContext[this.DataSource] as CurrencyManager;
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties( cm.Current );
e.Value = string.Format( "{0},{1}" ,
pdc[PropA].GetValue( cm.Current ) ,
pdc[PropB].GetValue( cm.Current ) );
}
}
Step 2: Use the MergeBinding:
MergeBinding
b = new MergeBinding(
"Text" , // The property of Textbox
employeeBindingSource , // The Binding source
string.Empty , // No member
"FirstName" , // Property one
"LastName" // Propert two
);
txbName.DataBindings.Add( b );
This code will merge property FirstName and LastName to one Textbox Control.

I will be in TechEd next week ( 9–11/5/2006).
I will give lecture a bout Data Binding in Depth.(Level 400)
How to exploit .NET 2.0 advanced binding features and write less code.
Date: 11/5/06 Time: 12:00 – 13:15

For customizing the behavior for a page or control, ASP.NET 2.0 lets you specify a ControlAdapter object that adapts or modifies behavior at key points in the life cycle of the control. At each stage in the life cycle, where a call to a life cycle method is made, the ASP.NET page framework checks to see if there is an associated adapter for the control and calls on the adapter's associated method instead of the control's method. In many cases, the adapter method may simply defer back to the control's method. An exception to this behavior are adapters for state management in that the adaptive behavior is additive to the control's state. for more info goto:
1.
Architectural Overview of Adaptive Control Behavior 2.
CSS friendly ASP.NET 2.0 control adapters Beta 1.1
More Posts