[]
        
(Showing Draft Content)

Combo Box Cell

Combo box cell displays an editable drop-down list within a cell. Users can expand the drop-down list by clicking the drop-down button, by pressing the F4, or Alt + Down arrow.


You can configure a combo box cell using the methods and properties of the ComboBoxCellType class in the GrapeCity.Wpf.SpreadSheet.CellType namespace. There are two primary methods of configuring items in a combo box cell:

  • Binding the combo box cell to a data source

    • Set the data source in the ItemsSource property.

    • Specify the field name to display the items in the ContentPath property.

    • Define the field name to be used as the cell value when an item is selected in the SelectedValuePath property.

    The following sample code binds a combo box cell to a ProductCollection().

    C#

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
         // Set items with data binding.
         GrapeCity.Wpf.SpreadSheet.CellType.ComboBoxCellType comboBound = new GrapeCity.Wpf.SpreadSheet.CellType.ComboBoxCellType
         {
              ItemsSource = new ProductCollection(),  
              ContentPath = "Name",
              SelectedValuePath = "ID"
         };
         spreadSheet1.Workbook.ActiveSheet.Columns[0].CellType = comboBound;
    }
    
    public class Product : System.ComponentModel.INotifyPropertyChanged
    {
         string id;
         string name;
         public string ID
         {
              set { id = value; NotifyPropertyChanged("ID"); }
              get { return id; }
         }
         public string Name
         {
              set { name = value; NotifyPropertyChanged("Name"); }
              get { return name; }
         }
         public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
         protected void NotifyPropertyChanged(string propertyName)
         {
              if (PropertyChanged != null)
              PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
         }
    }
    
    internal class ProductCollection : System.Collections.ObjectModel.ObservableCollection<Product>
    {
         public ProductCollection()
         {
              Add(new Product() { ID = "CUPTNS0001", Name = "John Miller" });
              Add(new Product() { ID = "CUPTND0002", Name = "Tom Martin" });
              Add(new Product() { ID = "CUPTNF0003", Name = "Paul Walker" });
              Add(new Product() { ID = "CUPSCO0004", Name = "Harry Shang" });
              Add(new Product() { ID = "CUPSCA0005", Name = "Joe Jonas" });
         }
    }

    VB

    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
    ' Set items with data binding.
    Dim comboBound As GrapeCity.Wpf.SpreadSheet.CellType.ComboBoxCellType = New GrapeCity.Wpf.SpreadSheet.CellType.ComboBoxCellType With {
                .ItemsSource = New ProductCollection(), 
                .ContentPath = "Name",
                .SelectedValuePath = "ID"
    }
    spreadSheet1.Workbook.ActiveSheet.Columns(0).CellType = comboBound
    End Sub
    
    Public Class Product
            Implements ComponentModel.INotifyPropertyChanged
            Private idField As String
            Private nameField As String
            Public Property ID As String
                Set(value As String)
                    idField = value
                    NotifyPropertyChanged("ID")
                End Set
                Get
                    Return idField
                End Get
            End Property
            Public Property Name As String
                Set(value As String)
                    nameField = value
                    NotifyPropertyChanged("Name")
                End Set
                Get
                    Return nameField
                End Get
            End Property
            Public Event PropertyChanged As ComponentModel.PropertyChangedEventHandler Implements ComponentModel.INotifyPropertyChanged.PropertyChanged
            Protected Sub NotifyPropertyChanged(propertyName As String)
                RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs(propertyName))
            End Sub
    End Class
    
    Friend Class ProductCollection
            Inherits ObjectModel.ObservableCollection(Of Product)
            Public Sub New()
                Add(New Product() With {
                .ID = "CUPTNS0001",
                .Name = "John Miller"
            })
                Add(New Product() With {
                .ID = "CUPTND0002",
                .Name = "Tom Martin"
            })
                Add(New Product() With {
                .ID = "CUPTNF0003",
                .Name = "Paul Walker"
            })
                Add(New Product() With {
                .ID = "CUPSCO0004",
                .Name = "Harry Shang"
            })
                Add(New Product() With {
                .ID = "CUPSCA0005",
                .Name = "Joe Jonas"
            })
            End Sub
    End Class
  • Setting unbound data in the data source

    • Use the Items property to reference the collection of items and the Add method to add item data.

    • If the combo box cell is not bound to a data source, the string displayed in the item will represent the cell's value.

      This behavior can be adjusted using the ValueType property.

    The following example code sets items in a combo box cell that are not bound to a data source.

    C#

    // Set unbound items.
    GrapeCity.Wpf.SpreadSheet.CellType.ComboBoxCellType comboUnbound = new GrapeCity.Wpf.SpreadSheet.CellType.ComboBoxCellType();
    comboUnbound.Items.Add("Japanese");
    comboUnbound.Items.Add("Korean");
    comboUnbound.Items.Add("Chinese");
    comboUnbound.Items.Add("German");
    comboUnbound.Items.Add("French");
    spreadSheet1.Workbook.ActiveSheet.Columns[2].CellType = comboUnbound;

    VB

    ' Set unbound items.
    Dim comboUnbound As GrapeCity.Wpf.SpreadSheet.CellType.ComboBoxCellType = New GrapeCity.Wpf.SpreadSheet.CellType.ComboBoxCellType()
    comboUnbound.Items.Add("Japanese")
    comboUnbound.Items.Add("Korean")
    comboUnbound.Items.Add("Chinese")
    comboUnbound.Items.Add("German")
    comboUnbound.Items.Add("French")
    spreadSheet1.Workbook.ActiveSheet.Columns(2).CellType = comboUnbound

Additionally, the drop-down list of a combo box cell can also display items in multiple columns. To enable the multi-column feature of a combo box cell and display items in multiple columns within the drop-down list, set the UseMultipleColumn property to true.

The following example code binds a combo box cell to a CellTypeCollection and enables the multi-column feature.

C#

private void Window_Loaded(object sender, RoutedEventArgs e)
{
     // Multi - column combo box.
     ComboBoxCellType c = new ComboBoxCellType();
     c.ItemsSource = new CellTypeCollection();
     c.ContentPath = "Name";
     c.UseMultipleColumn = true;
     c.DropDownWidth = 400;
     spreadSheet1.Workbook.ActiveSheet.Columns[0].CellType = c;
     spreadSheet1.Workbook.ActiveSheet.Columns[0].ColumnWidth = 400;
}

public class CellTypeCollection : List<SpreadCellType>
{
     public CellTypeCollection()
     {
            Add(new SpreadCellType()
            {
                Display = new BitmapImage(new Uri(@"Images\TextCellType.png", UriKind.Relative)),
                Name = "Text",
                Description = "Only permitted character types can be entered."
            });
            Add(new SpreadCellType()
            {
                Display = new BitmapImage(new Uri(@"Images\DateCellType.png", UriKind.Relative)),
                Name = "DateTime",
                Description = "You can enter a date and time."
            });
            Add(new SpreadCellType()
            {
                Display = new BitmapImage(new Uri(@"Images\NumberCellType.png", UriKind.Relative)),
                Name = "Number",
                Description = "You can enter a number."
            });
            Add(new SpreadCellType()
            {
                Display = new BitmapImage(new Uri(@"Images\ComboCellType.png", UriKind.Relative)),
                Name = "ComboBox",
                Description = "Select data from the combo box."
            });
            Add(new SpreadCellType()
            {
                Display = new BitmapImage(new Uri(@"Images\ButtonCellType.png", UriKind.Relative)),
                Name = "Button",
                Description = "Show the button."
            });
     }
}

VB

Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
    ' Multi - column combo box.
    Dim c As ComboBoxCellType = New ComboBoxCellType()
    c.ItemsSource = New CellTypeCollection()
    c.ContentPath = "Name"
    c.UseMultipleColumn = True
    c.DropDownWidth = 400
    spreadSheet1.Workbook.ActiveSheet.Columns(0).CellType = c
    spreadSheet1.Workbook.ActiveSheet.Columns(0).ColumnWidth = 400
End Sub

Public Class CellTypeCollection
    Inherits List(Of SpreadCellType)
    Public Sub New()
        Add(New SpreadCellType() With {
        .Display = New BitmapImage(New Uri("Images\TextCellType.png", UriKind.Relative)),
        .Name = "Text",
        .Description = "Only permitted character types can be entered."
        })
        Add(New SpreadCellType() With {
        .Display = New BitmapImage(New Uri("Images\DateCellType.png", UriKind.Relative)),
        .Name = "DateTime",
        .Description = "You can enter a date and time."
        })
        Add(New SpreadCellType() With {
        .Display = New BitmapImage(New Uri("Images\NumberCellType.png", UriKind.Relative)),
        .Name = "Number",
        .Description = "You can enter a number."
        })
        Add(New SpreadCellType() With {
        .Display = New BitmapImage(New Uri("Images\ComboCellType.png", UriKind.Relative)),
        .Name = "ComboBox",
        .Description = "Select data from the combo box."
        })
        Add(New SpreadCellType() With {
        .Display = New BitmapImage(New Uri("Images\ButtonCellType.png", UriKind.Relative)),
        .Name = "Button",
        .Description = "Show the button."
        })
    End Sub
End Class