Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Merge Multi Colums in Binding

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] );
             }
        }
}

2 Comments

  • Just goes to show the importance of comments - not to mention an explanation. This code wouldn't pass code review.

  • Try this instead

    ///
    /// Initializes a new instance of the MultipleBinding class.
    ///
    /// The name of the control property to bind.

    /// An Object that represents the data source.

    /// The property or list to bind to.

    /// A format string to use. Each dataMember must be encased in {}. Standard String format specifiers may be used. e.g. {Weight:D5}

    public MultipleBinding(string propertyName, object dataSource, string dataMember, string bindingString)
    : base(propertyName, dataSource, dataMember)
    {
    _bindingString = bindingString;

    // All other dataMembers besides the main one must be subscribed to the changed event to pick up changes
    String[] props = FormatProperties();
    foreach (string prop in props)
    {
    if (prop != dataMember)
    {
    EventDescriptorCollection events = TypeDescriptor.GetEvents(dataSource);
    EventDescriptor e = events.Find(prop + "Changed", true);
    if (e != null)
    e.AddEventHandler(dataSource, new EventHandler(PropertyChanged));
    }
    }
    }

    private void PropertyChanged (object sender, EventArgs e)
    {
    ReadValue();
    }

    // Returns a list of properties that are referenced in the format string
    private string[] FormatProperties()
    {
    System.Collections.Generic.List temp = new List();

    string s = _bindingString;
    int i = 0;
    while (i >= 0)
    {
    i = s.IndexOf("{", i);
    if (i >= 0)
    {
    string prop = s.Substring(i + 1).Split('}')[0];
    int j = prop.IndexOf(":");
    if (j > 0) prop = prop.Substring(0, j);
    temp.Add(prop);
    i++;
    }
    }

    return temp.ToArray();
    }

    // Returns a list of format modifiers that are used in the format string
    private string[] FormatModifiers()
    {
    System.Collections.Generic.List temp = new List();

    string s = _bindingString;
    int i = 0;
    while (i >= 0)
    {
    i = s.IndexOf("{", i);
    if (i >= 0)
    {
    string prop = s.Substring(i + 1).Split('}')[0];
    int j = prop.IndexOf(":");
    if (j > 0)
    prop = prop.Substring(j);
    else
    prop = "";
    temp.Add(prop);
    i++;
    }
    }

    return temp.ToArray();
    }

    ///
    /// Generates the bound text based on the format string.
    ///
    /// A ConvertEventArgs that contains the event data.

    protected override void OnFormat( ConvertEventArgs e )
    {
    // The bound object
    Object cur = this.BindableComponent.BindingContext[this.DataSource].Current;
    // A list of all its properties
    PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(cur);

    // The string we are building
    string s = _bindingString;

    string[] props = FormatProperties();
    string[] formats = FormatModifiers();

    for (int i = 0; i 0)
    {
    val = string.Format("{0" + formats[i] + "}", pdc[props[i]].GetValue(cur));
    s = s.Replace("{" + props[i] + formats[i] + "}", val);
    }
    else
    {
    // Just get the property
    val = pdc[props[i]].GetValue(cur).ToString();
    s = s.Replace("{" + props[i] + "}", val);
    }

    }
    e.Value = s;
    }
    }

Comments have been disabled for this content.