Xceed Grid for WinForms v4.3 Documentation
Display format

Welcome to Xceed Grid for WinForms v4.3 > Basic Concepts > Editor Controls > WinComboBox control > Display format

When an item is selected, by default, its values will be displayed as comma separated items. The format with which the SelectedItem's values are displayed is defined by the DisplayFormat property. The default display format is the name (Name) of each column, preceded and terminated with a percent (%), and separated by a comma (,). For example, if the combobox contains 4 columns named "Country", "Population", "City", and "AverageTemp", the display format will be "%Country%, %Population%, %City%, %AverageTemp%". When an item is selected, the display format will be resolved to the values of the correspond columns. For example: "Germany, 83000000, Berlin, 25.1 ºc". 

 

To change the format with which the selected item's text is displayed, the DisplayFormat property can be set to the desired format. For example, in the code below, the DisplayFormat property was changed to display only some of the selected item's values and display them as phrase. 

VB.NET
Copy Code
Dim combo As New WinComboBox()
combo.Columns.Add( New ColumnInfo( "Country", GetType( String ) ) )
combo.Columns.Add( New ColumnInfo( "Population", GetType( Integer ) ) )      
combo.Columns.Add( New ColumnInfo( "City", GetType( String ) ) )
combo.Columns.Add( New ColumnInfo( "AverageTemp", GetType(   String   ) ) )    
combo.Items.Add( "Canada", 31500000, "Ottawa", "12.2 ºc" )
combo.Items.Add( "Switzerland", 7300000, "Bern", "23.3 ºc" )
combo.Items.Add( "France", 59500000, "Paris", "27.3 ºc" )
combo.Items.Add( "USA", 278000000, "Washington", "14.1 ºc" )
combo.Items.Add( "UK", 59700000, "London", "23.7 ºc" )
combo.Items.Add( "Belgium", 10300000, "Brussels", "21.8 ºc" )
combo.Items.Add( "Italy", 57700000, "Rome", "29.6 ºc" )
combo.Items.Add( "Spain", 40000000, "Madrid", "31.8 ºc" )
combo.Items.Add( "Germany", 83000000, "Berlin", "25.1 ºc" )
combo.Items.Add( "Japan", 126800000, "Tokyo", "17.2 ºc"   )
combo.DisplayFormat = "%Country% has a population of %Population%"
combo.Location = New Point( 10 , 10 )
Me.Controls.Add( combo )
C#
Copy Code
WinComboBox combo = new WinComboBox();
combo.Columns.Add( new ColumnInfo( "Country", typeof( string ) ) );
combo.Columns.Add( new ColumnInfo( "Population", typeof( int ) ) );     
combo.Columns.Add( new ColumnInfo( "City", typeof( string ) ) );
combo.Columns.Add( new ColumnInfo( "AverageTemp", typeof(   string   ) ) );    
combo.Items.Add( "Canada", 31500000, "Ottawa", "12.2 ºc" ) ;
combo.Items.Add( "Switzerland", 7300000, "Bern", "23.3 ºc" );
combo.Items.Add( "France", 59500000, "Paris", "27.3 ºc" );
combo.Items.Add( "USA", 278000000, "Washington", "14.1 ºc" );
combo.Items.Add( "UK", 59700000, "London", "23.7 ºc" );
combo.Items.Add( "Belgium", 10300000, "Brussels", "21.8 ºc" );
combo.Items.Add( "Italy", 57700000, "Rome", "29.6 ºc" );
combo.Items.Add( "Spain", 40000000, "Madrid", "31.8 ºc" );
combo.Items.Add( "Germany", 83000000, "Berlin", "25.1 ºc" );
combo.Items.Add( "Japan", 126800000, "Tokyo", "17.2 ºc" );
combo.DisplayFormat = "%Country% has a population of %Population%" ;
combo.Location = new Point( 10 , 10 );
this.Controls.Add( combo );

If one of the variables used in the DisplayFormat is not resolvable, it will remain as is. For example, if "%Cntry% has a population of %Population%" is assigned to the DisplayFormat property (notice the spelling mistake in the first variable: "Cntry" rather than "Country"), when the selected item's text/value is displayed, the result will be: "%Cntry/ has a population of 83000000". 

In the case where the WinComboBox control is created using the constructor that accepts a DisplayFormat, only the columns specified by the DisplayFormat parameter will be created in the DropDownControl.

QueryItemText event

The format with which the selected item's text/values are displayed can also be modified using the QueryItemText event. This event is raised every time the WinComboBox control needs to display the selected item's text, and can be used to either format the text of each selected item, or provide adjustments to the current DisplayFormat. 

In the example below, the DisplayFormat property has been set to display each value in the selected item; however, because some of the values are empty, the formatting needs to be adjusted via the QueryItemText event.

VB.NET
Copy Code
Dim combo As New WinComboBox()
combo.Columns.Add( New ColumnInfo( "LastName", GetType( String ), 0, _
                                   ColumnSortDirection.Ascending ) )
combo.Columns.Add( New ColumnInfo( "FirstName", GetType( String ) ) )
combo.Columns.Add( New ColumnInfo( "Occupation", GetType( String ) ) )
combo.Items.Add( "Ledoux", "Pierre-Luc", "Developer" )
combo.Items.Add( "Drimonakos", "Matt", "Technical Support" )
combo.Items.Add( "Beland", "Jenny", "Technical Writer" )
combo.Items.Add( "Carignan", "Francois", "Developer" )
combo.Items.Add( "Kacem", string.Empty, "Webmaster" )
combo.Items.Add( string.Empty, "Normand", "Webmaster" )
combo.Items.Add( "Delva", "Jean-Bernard", "Developer" )
combo.Items.Add( "Duncan", "Charles", "Sales and Marketing" )
combo.Items.Add( "Redknap", "David", string.Empty )
combo.Items.Add( string.Empty, "Vincent", "Developer" )
combo.Items.Add( "Fortin", "Michel", "Developer" )
combo.Items.Add( "Bourque", "Jacques", "Network Adminstrator" )
combo.Items.Add( "Bourque", "Pascal", "Developer" )
combo.Items.Add( "Plante", "Martin", "Developer" )
combo.Items.Add( "Kosmatos", "Odi", "Sales and Marketing" )
combo.Items.Add( "Cote", string.Empty, "President" )
AddHandler combo.QueryItemText, AddressOf Me.FormatText
combo.DisplayFormat = "%FirstName% %LastName% %Occupation%"     
combo.Location = New Point( 10 , 10 )
Me.Controls.Add( combo )
Private Sub FormatText( ByVal sender As Object, ByVal e As QueryItemTextEventArgs )
  If Not e.Item Is Nothing Then
    If CType( e.Item( "Occupation" ) ) = String.Empty Then
      e.Text = CType( e.Item( "FirstName" ), String ) & " " & CType( e.Item( "LastName" ), String )
    End If
    e.Text = e.Text.Trim()
  End If     
End Sub
C#
Copy Code
WinComboBox combo = new WinComboBox();   
combo.Columns.Add( new ColumnInfo( "LastName", typeof( string ), 0,
                                   ColumnSortDirection.Ascending ) );
combo.Columns.Add( new ColumnInfo( "FirstName", typeof( string ) ) );
combo.Columns.Add( new ColumnInfo( "Occupation", typeof( string ) ) );
combo.Items.Add( "Ledoux", "Pierre-Luc", "Developer" );
combo.Items.Add( "Drimonakos", "Matt", "Technical Support" );
combo.Items.Add( "Beland", "Jenny", "Technical Writer" );
combo.Items.Add( "Carignan", "Francois", "Developer" );
combo.Items.Add( "Kacem", string.Empty, "Webmaster" );
combo.Items.Add( string.Empty, "Normand", "Webmaster" );
combo.Items.Add( "Delva", "Jean-Bernard", "Developer" );
combo.Items.Add( "Duncan", "Charles", "Sales and Marketing" );
combo.Items.Add( "Redknap", "David", string.Empty );
combo.Items.Add( string.Empty, "Vincent", "Developer" );
combo.Items.Add( "Fortin", "Michel", "Developer" );
combo.Items.Add( "Bourque", "Jacques", "Network Adminstrator" );
combo.Items.Add( "Bourque", "Pascal", "Developer" );
combo.Items.Add( "Plante", "Martin", "Developer" );
combo.Items.Add( "Kosmatos", "Odi", "Sales and Marketing" );
combo.Items.Add( "Cote", string.Empty, "President" );
combo.QueryItemText += new QueryItemTextEventHandler( this.FormatText );
combo.DisplayFormat = "%FirstName% %LastName% %Occupation%";     
combo.Location = new Point( 10, 10 );
this.Controls.Add( combo );
private void FormatText( object sender, QueryItemTextEventArgs e )
{
  if( e.Item != null )
  {
    if( ( string )e.Item[ "Occupation" ] == string.Empty )
    {
       e.Text = ( string )e.Item[ "FirstName" ] + " " + ( string )e.Item[ "LastName" ];
    }
    e.Text = e.Text.Trim();
  }     
}
In the previous examples, ColumnInfo objects were added prior to adding items to the combobox. In the case where items are added without first adding ColumnInfo objects, the columns will have the default names of "Column1, Column2, etc.". 

In the case where the combobox is bound to a data source, the column names will be the names of the fields in the data source. In the case where the data source is a jagged array, the column names will be "0", "1", "2", etc.