Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How can I use simple binding to display multiple data source properties?

Simple binding does not directly support data source property concatenation however you can use the Binding Format event to concatenate multiple data source properties.  To do this you need to add a Binding to the primary property (or one of the data source properties you want to concatenate) and use the Format event to concatenate the multiple property values.  Note that although this will generally work as expected, there are times where the Format event may not produce the desired results.  Since the binding is bound to a single data source property, the Control will only update when the bound data source property changes.  If you need the Control to update when either property changes, then you will need to write additional code to update the Control when the secondary property changes.

Sample: Concatenating data source properties using the Format event (VS 2005) (VS Project: MultiColumnSimpleBinding)

/* Setup binding - bind to Hour */
/* Second would be a better choice but this provides a better example */
/* of the potential issues you can run into with multi-column binding */
this.textBox1.DataBindings.Add("Text", bo, "Hour", true);

/* Add formatting event */
Binding timeBinding = this.textBox1.DataBindings["Text"];

/* Concatenate "Hour", "Minute" and "Second" properties on the data source */
timeBinding.Format += delegate(object binding, ConvertEventArgs args)
{
    // Get the bound object
    BusObject   bo = ((binding as Binding).BindingManagerBase.Current as BusObject);

    if (null != bo)
    {
   args.Value = string.Format("{0:00}:{1:00}:{2:00}", bo.Hour, bo.Minute, bo.Second);
    }
};