Merge Two Colums in Binding
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.