Click here to Skip to main content
14,977,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am pretty new to wpf and kinda struggling with the way it works. I would like to create a DataGrid with two datagridcomboboxcolumn fields with the comboboxcolumns being populated from an arraylist.

The datagrid takes itemssource from a datatable and the list of values in the two combobox columns should be two different lists. By default, the value in the datagrid itemsource should be selected in these two comboboxes.

Can any one tell me the step by step procedure to do this? I have tried many ways and all failed.

Thanks in advance
Posted
Updated 25-Dec-12 23:36pm
v4

1 solution

Hi, you can achive this with help of usage MVVM and ocre of a bindings in WPF architecture.

So lets pretend that you have class (this class would be your Model in MVVM terminology), for example:
C#
public class MyTestData
{
  public int ID {get;set;}
  public string[] Array1 {get;set}
  public string[] Array2 {get;set}
}


then lets declare your ViemModel
C#
public class MainVM:Galasoft.MvvmLight.ViewModelBase
{
  public MainVM():base{
ListOfData =new ObservableCollection<mytestdata>();
//there you can add some fake data
}

  public ObservableCollection<mytestdata> ListOfData {get;set;}
}</mytestdata></mytestdata>


and the last one, i'm going to define you View (XAML)

XML
<datagrid datacontext="{Binding local:MainVM}" itemssource="{Binding ListOfData}">
<datagrid.columns>
<datagridtemplatecolumn height="100" header="Array1">
   <datagridtemplatecolumn.celltemplate>
     <datatemplate> 
       <combobox itemssource="{Binding Array1}" />
     </datatemplate>
    </datagridtemplatecolumn.celltemplate>
</datagridtemplatecolumn>
<datagridtemplatecolumn height="100" header="Array2">
   <datagridtemplatecolumn.celltemplate>
     <datatemplate> 
       <combobox itemssource="{Binding Array2}" />
     </datatemplate>
    </datagridtemplatecolumn.celltemplate>
</datagridtemplatecolumn>
</datagrid.columns>
</datagrid>


Hope this this would be helpful for U,
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900