How to: Assign a ComboBox Editor to a Column

  • 3 minutes to read

This example shows how to assign the combo box and spin editors to grid columns.

<Window x:Class="DXGrid_AssignComboBoxToColumn.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" 
        xmlns:local="clr-namespace:DXGrid_AssignComboBoxToColumn" 
        Title="Window1" Height="300" Width="499">
    <Grid>
        <dxg:GridControl x:Name="grid" ItemsSource="{Binding PersonList}">
            <dxg:GridControl.Columns>
                <dxg:GridColumn FieldName="ProductName" />
                <dxg:GridColumn FieldName="UnitPrice">
                    <dxg:GridColumn.EditSettings>
                        <dxe:SpinEditSettings MaxValue="999" MinValue="1" />
                    </dxg:GridColumn.EditSettings>
                </dxg:GridColumn>
                <dxg:GridColumn FieldName="Type" >
                    <dxg:GridColumn.EditSettings>
                        <dxe:ComboBoxEditSettings ItemsSource="{Binding TypeList}" 
                                                  DisplayMember="TypeName" ValueMember="Id"/>
                    </dxg:GridColumn.EditSettings>
                </dxg:GridColumn>
            </dxg:GridControl.Columns>
            <dxg:GridControl.View>
                <dxg:TableView AutoWidth="True" />
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</Window>
using System.Collections.ObjectModel;
using System.Windows;

namespace DXGrid_AssignComboBoxToColumn {
    public partial class Window1 : Window {
        public Window1() {
            InitializeComponent();
            this.DataContext = new MyViewModel();
        }
    }
    public class MyViewModel {
        public MyViewModel() {
            CreateList();
        }

        public ObservableCollection<Person> PersonList { get; set; }
        public ObservableCollection<ProductType> TypeList { get; set; }
        void CreateList() {
            PersonList = new ObservableCollection<Person>();
            for (int i = 0; i < 20; i++) {
                Person p = new Person(i);
                PersonList.Add(p);
            }
            TypeList = new ObservableCollection<ProductType>();
            for (int i = 0; i < 3; i++) {
                TypeList.Add(new ProductType(i));
            }
        }
    }
    public class Person {
        public Person(int i) {
            ProductName = "FirstName" + i;

            UnitPrice = i;
            Type = 0;
        }

        public string ProductName { get; set; }

        public int Type { get; set; }

        public int UnitPrice { get; set; }
    }

    public class ProductType {
        public ProductType(int i) {
            Id = i;
            TypeName = "Type" + i;
        }
        public int Id { get; set; }
        public string TypeName { get; set; }
    }
}