This project has moved. For the latest updates, please go here.

Populate ComboBox in WPF DataGrid

Jun 9, 2014 at 4:00 PM
Hi,

I am using wpftoolkit v2.0. I want sample code for populating combobox in datagrid which bound with datatable and set selected value from object data source.

Thanks
Developer
Jun 10, 2014 at 5:29 PM
Hi,

Maybe something like :
<Window.Resources>
      <xcdg:DataGridCollectionViewSource x:Key="cvsRandomData"
                                         Source="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=local:MainWindow},Path=RandomData}" />

      <xcdg:CellEditor x:Key="comboEditor">
         <xcdg:CellEditor.EditTemplate>
            <DataTemplate>
               <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=local:MainWindow},Path=Ranks}"
                         SelectedValue="{xcdg:CellEditorBinding}">
               </ComboBox>
            </DataTemplate>
         </xcdg:CellEditor.EditTemplate>
      </xcdg:CellEditor>
   </Window.Resources>
    <Grid>
      <xcdg:DataGridControl x:Name="_dataGrid"
                            ItemsSource="{Binding Source={StaticResource cvsRandomData}}"
                            SelectedItem="{Binding  RelativeSource={RelativeSource FindAncestor,AncestorType=local:MainWindow},Path=SelectedItem}">
         <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="Name"
                         Width="100" />
            <xcdg:Column FieldName="Rank"
                         Width="150"
                         CellEditor="{StaticResource comboEditor}"/>
            <xcdg:Column FieldName="Value"
                         Width="75" />
         </xcdg:DataGridControl.Columns>
      </xcdg:DataGridControl>
   </Grid>
</Window>

namespace WpfApplication70
{
  public partial class MainWindow : Window
  {
    private Random randomizer = new Random();
    private List<string> ranksList = new List<string>() { "First", "Second", "Third" };

    public MainWindow()
    {
      InitializeComponent();
      
      dataTable.Columns.Add( new DataColumn( "Name", typeof( string ) ) );
      dataTable.Columns.Add( new DataColumn( "Rank", typeof( string ) ) );
      dataTable.Columns.Add( new DataColumn( "Value", typeof( int ) ) );

      for( int i = 0 ; i < 30 ; i++ )
      {
        object[] values = new object[]  {  "Name" + i + 1, this.GetRandomDataEnum(), i * 6 };
        dataTable.Rows.Add( values );
      }
    }

    public DataTable RandomData
    {
      get
      {
        return dataTable;
      }
    }
    DataTable dataTable = new DataTable();

    public List<String> Ranks
    {
      get
      {
        return ranksList;
      }
    }

    public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register( "SelectedItem", typeof( object ), typeof( MainWindow ), new UIPropertyMetadata( null, OnSelectedItemChanged ) );
    public MyItems SelectedItem
    {
      get
      {
        return ( MyItems )GetValue( SelectedItemProperty );
      }
      set
      {
        SetValue( SelectedItemProperty, value );
      }
    }

    private static void OnSelectedItemChanged( DependencyObject o, DependencyPropertyChangedEventArgs e )
    {
      MainWindow window = o as MainWindow;
      if( window != null )
      {
      }
    }

    private string GetRandomDataEnum()
    {
      int value = randomizer.Next( 0, 3 );
      return ranksList[ value ];
    }
  }

  public class MyItems
  {
    public string Name { get; set; }
    public string Rank { get; set; }
    public int Value { get; set; }
  }
}