Click here to Skip to main content
11,492,385 members (61,562 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
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 25-Dec-12 21:59pm
Edited 26-Dec-12 0:36am
v4

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
public class MyTestData
{
  public int ID {get;set;}
  public string[] Array1 {get;set}
  public string[] Array2 {get;set}
}

then lets declare your ViemModel
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)

<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,
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 399
1 Maciej Los 266
2 KarstenK 210
3 CPallini 208
4 Sascha Lefèvre 195
0 Sergey Alexandrovich Kryukov 9,943
1 OriginalGriff 8,610
2 Sascha Lefèvre 3,609
3 Maciej Los 3,372
4 Richard Deeming 2,440


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 26 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100