none
Using a combobox inside a datagrid (silverlight 4 RC)

    Question

  • Hi,

    This is my xaml where I try to use a combobox inside a datagrid:

    ....
    <
    sdk:DataGrid.Columns
    >
    <sdk:DataGridTemplateColumn x:Name="projectIDColumn" Header="Project ID" Width
    ="SizeToHeader">
    <sdk:DataGridTemplateColumn.CellEditingTemplate
    >
    <DataTemplate
    >
    <ComboBox x:Name
    ="cboProjects"
    SelectedValue="{Binding Mode=TwoWay,Path=ProjectID}"

    DisplayMemberPath="Name"

    SelectedValuePath="ProjectID"

    ItemsSource="{Binding ElementName=projectDomainDataSource, Path=Data,Mode=TwoWay}"
    >
    </ComboBox
    >
    </DataTemplate
    >
    </sdk:DataGridTemplateColumn.CellEditingTemplate
    >
    </sdk:DataGridTemplateColumn>

    ....

    The combo is not showing any data in the list.
    When I put the combobox outside the datagrid it working as expected.

    What could be wrong?

    Thanks

    Monday, March 22, 2010 3:11 PM

Answers

  • Here are the pieces [One Important Comment - This is based on WCF RIA RC with SL4 RC]

    public class EntitiesLookupProvider
    {
        public static XXXXDomainContext Context = new XXXXDomainContext();
        public EntitySet<Entity> Entities
        {
            get
            {
                Context.Load(Context.GetEntitiesQuery(), false);
                return Context.Entities;
            }
        }
    }
     
    <Grid.Resources>
        <local:EntitiesLookupProvider x:Name="LookupProvider_Entities" x:Key="LookupProvider_Entities"/>
    </Grid.Resources>
    
    public ObservableCollection<Entity> Ref_LookupProvider_Entities = new ObservableCollection<Entity>(); 
    
    <riacontrols:DomainDataSource AutoLoad="True" Height="0" LoadingData="DDS_LoadingData" LoadedData="EntityDomainDataSource_LoadedData" SubmittingChanges="DDS_SubmittingChanges" SubmittedChanges="DDS_SubmittedChanges" Name="EntityDomainDataSource" QueryName="GetEntitiesQuery" Width="0">
      <riacontrols:DomainDataSource.DomainContext>
           <webservices:XXXXDomainContext />
       </riacontrols:DomainDataSource.DomainContext>
    </riacontrols:DomainDataSource>
     
    <datagrid:DataGridTemplateColumn x:Name="EntityNameColumn" Header="Entity" Width="150">
      <datagrid:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
             <TextBlock x:Name="Entity" Text="{Binding Path=EntityName}" Height="23" Margin="3" />
        </DataTemplate>
      </datagrid:DataGridTemplateColumn.CellTemplate>
      <datagrid:DataGridTemplateColumn.CellEditingTemplate>
         <DataTemplate>
             <ComboBox x:Name="CB_Entity"
                  Width="150"
                  SelectionChanged="CB_Entity_SelectionChanged"
                  ItemsSource="{Binding Path=Entities, Source={StaticResource LookupProvider_Entities}}" 
                  SelectedValuePath="PK"
                  SelectedValue="{Binding Entity, Mode=TwoWay}"
                  DisplayMemberPath="Name">
               <ComboBoxItem/>
           </ComboBox>
        </DataTemplate>
     </datagrid:DataGridTemplateColumn.CellEditingTemplate>
    <datagrid:DataGridTemplateColumn>
    
    Friday, March 26, 2010 2:08 PM

All replies

  • You want to use <datagrid:DataGridTemplateColumn.CellTemplate> instead of <datagrid:DataGridTemplateColumn.CellEditingTemplate> unless you want separate display and editing components in the cell, then use both.

    Marc

    Monday, March 22, 2010 4:52 PM
  • You are right Marc, but the problem is the same

    Bart

    Tuesday, March 23, 2010 4:52 AM
  • Is ProjectID a value in both sets of entities, DataGrid and ComboBox ItemsSources?

    Marc  

    Tuesday, March 23, 2010 7:07 AM
  • To make ComboBox binding work in DataGrid, seems using StaticResource is the only way. Take a look at this blog to see how to convert data coming from DB to StaticResource then fill the ComboBox:

    http://weblogs.asp.net/manishdalal/archive/2008/09/28/combobox-in-datagrid.aspx 

     

     

     

    Tuesday, March 23, 2010 6:00 PM
  • Yes, ProjectID is the field name in the master table and a foreign key in the table for the combo.

     

     

    Wednesday, March 24, 2010 4:58 PM
  • A static resourc is indeed working, but I'm using a domainDataSource:

     <riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:Project, CreateList=true}" Height="0" LoadedData="projectDomainDataSource_LoadedData" Name="projectDomainDataSource" QueryName="GetProjectsQuery" Width="0">

    <riaControls:DomainDataSource.DomainContext>

    <my:ContractorContext />

    </riaControls:DomainDataSource.DomainContext>

    </riaControls:DomainDataSource>

    Wednesday, March 24, 2010 5:06 PM
  • I agree with sladapter. The datagrid itself is what should be updated and can be directly linked to a DDS. The staticresources should be used for the combobox selections but can be initially loaded from a DDS and not necessarily handcoded. You can use events to update the staticresource collections if they are dynamically changing and reset the itemssource of the combobox to reflect the changes if that is necessary. That is what I am doing.

    Marc

    Wednesday, March 24, 2010 5:26 PM
  • Then you have to change it to use StaticResources, Just fill the LookupProvider with the DomainDataSource data. You can even have an application level of LookupManager to store all the Lookup list in a Dictionary so you do not even need to retrieve them again when you need it in your other page:

        public class LookupProvider : ObservableDictionary<string, IEnumerable>
        {
            static LookupProvider _instance = new LookupProvider();
            public static LookupProvider Instance
            {
                get { return LookupProvider; }
            }         
        }

     In Your App.Xaml.cs:

    private void Application_Startup(object sender, StartupEventArgs e)
            {          
                this.Resources.Add("LookupProvider", LookupProvider.Instance);     

                ...      
              
            }

    Then on the ComboBox, you can bind ItemsSource this way:

    <ComboBox ItemsSource ="{Binding Path=[YourLookupName], Source={StaticResource LookupManager}}" .../>

    On your projectDomainDataSource_LoadedData Function:

     void ds_LoadedData(object sender, LoadedDataEventArgs e)
            {
                DomainDataSource ds = sender as DomainDataSource;
                if (e.HasError)
                {
                }
                else
                {

                     if(!LookupProvider.Instance.ContainsKey("YourLookupName"))
                            LookupProvider.Instance.Add("YourLookupName", ds.Data);

                     else

                           LookupProvider.Instance["YourLookupName"] = ds.Data;  // Replace old list if needed.
                }
            }

    The only thing missing here is that we do not have a Dictionary that implemented INotifyPropertyChanged interface. So you won't see the ComboBox been populated when the Data comes back if you just use regular Dictionary object to hold the lookup list.

    You need a some kind of ObservableDictionary. Here is a one I wrote:

    public class ObservableDictionary<TKey, TValue> : IDictionary< TKey, TValue>,  INotifyCollectionChanged, INotifyPropertyChanged      
        {
            public event NotifyCollectionChangedEventHandler CollectionChanged;
            public event PropertyChangedEventHandler PropertyChanged;

            private Dictionary<TKey, TValue> dic = new Dictionary<TKey, TValue>();
            protected virtual void OnPropertyChanged(params string[] properties)
            {
                if (PropertyChanged != null)
                {
                    foreach (string p in properties)
                        PropertyChanged(this, new PropertyChangedEventArgs(p));
                }
            }
            protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
            {          
                  if(CollectionChanged!=null)
                    CollectionChanged(this, args);         
            }
              
            public void Add(TKey key, TValue value)
            {
                if (dic.Keys.Contains(key))
                    return;
                KeyValuePair<TKey, TValue> item = new KeyValuePair<TKey, TValue>(key, value);
                Add(item);           
            }

            public bool ContainsKey(TKey key)
            {
                return dic.Keys.Contains(key);
            }

            public ICollection<TKey> Keys
            {
                get { return dic.Keys; }
            }

            public bool Remove(TKey key)
            {
                bool result = dic.Remove(key);
                if(result)
                    OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                return result;
            }

            public bool TryGetValue(TKey key, out TValue value)
            {
                return dic.TryGetValue(key, out value);
            }

            public ICollection<TValue> Values
            {
                get { return dic.Values; }
            }

            public virtual TValue this[TKey key]
            {
                get
                {
                    if(dic.ContainsKey(key))
                        return dic[key];
                    return default(TValue);
                }
                set
                {
                    dic[key] = value;
                    OnPropertyChanged(key.ToString());
                }
            }

            public void Add(KeyValuePair<TKey, TValue> item)
            {
                dic.Add(item.Key, item.Value);
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, dic.Count - 1));
            }

            public void Clear()
            {
                dic.Clear();
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }

            public bool Contains(KeyValuePair<TKey, TValue> item)
            {
                return (this.ContainsKey(item.Key) && dic[item.Key].Equals(item.Value));
            }

            public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
            {
                throw new NotImplementedException();
            }

            public int Count
            {
                get { return dic.Count; }
            }

            public bool IsReadOnly
            {
                get { return false; }
            }

            public bool Remove(KeyValuePair<TKey, TValue> item)
            {
                bool result = Remove(item.Key);           
                if(result)
                    OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, -1));
                return result;
            }

            public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
            {
                return dic.GetEnumerator();
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return dic.GetEnumerator();
            }
        }


    Wednesday, March 24, 2010 5:43 PM
  • Hi Marc, When I create a staticresource with DDS, like this:

    <navigation:Page.Resources>
      <my:ContractorContext x:Key="Projects"/>
    </navigation:Page.Resources>

    .....

    <ComboBox ItemsSource="{Binding Projects, Source={StaticResource Projects}}"
    SelectedValuePath="{Binding Mode=TwoWay, Path=ProjectID.Value}"
    DisplayMemberPath="Name"/>

    .....

     

    Thursday, March 25, 2010 7:49 AM
  • Hi Sladapter, the itemssource is null when I debug you approach.
    It seems that the path [YourLookupName] is returning null.
    I'm I missing something?
    When I create a new class with a function for the statisresource and that look's for the "YourLookupName" in the LookupProvider then I got it working.
    Only the binding with the SelectedValuePath="{Binding Mode=TwoWay, Path=ProjectID.Value} is not working, the selected item is always the first one

    Thursday, March 25, 2010 7:58 AM
  • Hi Sladapter, the itemssource is null when I debug you approach.
    It seems that the path [YourLookupName] is returning null.
    I'm I missing something?

     

    1) Did you put a break point at the following line in your DataSource LoadedData event to see if the LookupProvider get filled?

    LookupProvider.Instance.Add("YourLookupName", ds.Data);

     2) Did you notice I changed the code a little (since I first posted it)? Put the LookupProvider.Instance in the Application.Resources in Application_Startup function instead in the App.XAML. This way we have only one Instance of LookupProvider in the whole application. No matter if you refer it in code or in XAML.

     

     

    Thursday, March 25, 2010 9:46 AM
  • I really like your obserabledictionary, it is a lot simpler than david poll's one http://www.davidpoll.com/2010/03/15/new-in-the-silverlight-4-rc-xaml-features/ good job.

    Thursday, March 25, 2010 7:28 PM
  • I really like your obserabledictionary, it is a lot simpler than david poll's one http://www.davidpoll.com/2010/03/15/new-in-the-silverlight-4-rc-xaml-features/ good job.

    Thanks. I was wondering why they did not provide this ObservableDictionary in the base code. It is really not that hard to implement, and it is really useful with Silverlight data binding. After I wrote it, I found so many places I could use it.

     

    Thursday, March 25, 2010 9:09 PM
  • I use ObservableCollection as my base class; its collection is Items. Sladapters's LookupProvider is based on ObservableDictionary which is derived from Dictionary and it has two collections: Keys and Values. ComboBox uses IEnumerator to retrieve the collection when the ItemsSource is assigned. The path would need to be the correct collection attribute's name, "[YourLookupName]" = Keys or Values; they implement IEnumerator.

    Marc

    Friday, March 26, 2010 6:00 AM
  • Yes, the data is loaded into the lookupProvider.
    When I set a breakpoint on ComboBox_Loaded event, the itemsource property is Null, when explicit set at that time the itemsource the list filled, only the binding is not working yet.

    This is fixed, i had still a page resource with the name "LookupProvider"

    The only problem left is the binding the to master table. The item in the combo is always the first one in the list, it also not updating when I select one.

    Do you have a working example?

    Thanks

    Bart

    Friday, March 26, 2010 6:09 AM
  • Try {Binding Keys, ... or {Binding Values,... not Projects.

    Friday, March 26, 2010 6:18 AM
  • Do you know somewhere an example with the northwind database, EF4.0, RIA services and a combobox in a datagrid?

    I'm getting desperate...Crying

     

    Friday, March 26, 2010 1:32 PM
  • Here are the pieces [One Important Comment - This is based on WCF RIA RC with SL4 RC]

    public class EntitiesLookupProvider
    {
        public static XXXXDomainContext Context = new XXXXDomainContext();
        public EntitySet<Entity> Entities
        {
            get
            {
                Context.Load(Context.GetEntitiesQuery(), false);
                return Context.Entities;
            }
        }
    }
     
    <Grid.Resources>
        <local:EntitiesLookupProvider x:Name="LookupProvider_Entities" x:Key="LookupProvider_Entities"/>
    </Grid.Resources>
    
    public ObservableCollection<Entity> Ref_LookupProvider_Entities = new ObservableCollection<Entity>(); 
    
    <riacontrols:DomainDataSource AutoLoad="True" Height="0" LoadingData="DDS_LoadingData" LoadedData="EntityDomainDataSource_LoadedData" SubmittingChanges="DDS_SubmittingChanges" SubmittedChanges="DDS_SubmittedChanges" Name="EntityDomainDataSource" QueryName="GetEntitiesQuery" Width="0">
      <riacontrols:DomainDataSource.DomainContext>
           <webservices:XXXXDomainContext />
       </riacontrols:DomainDataSource.DomainContext>
    </riacontrols:DomainDataSource>
     
    <datagrid:DataGridTemplateColumn x:Name="EntityNameColumn" Header="Entity" Width="150">
      <datagrid:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
             <TextBlock x:Name="Entity" Text="{Binding Path=EntityName}" Height="23" Margin="3" />
        </DataTemplate>
      </datagrid:DataGridTemplateColumn.CellTemplate>
      <datagrid:DataGridTemplateColumn.CellEditingTemplate>
         <DataTemplate>
             <ComboBox x:Name="CB_Entity"
                  Width="150"
                  SelectionChanged="CB_Entity_SelectionChanged"
                  ItemsSource="{Binding Path=Entities, Source={StaticResource LookupProvider_Entities}}" 
                  SelectedValuePath="PK"
                  SelectedValue="{Binding Entity, Mode=TwoWay}"
                  DisplayMemberPath="Name">
               <ComboBoxItem/>
           </ComboBox>
        </DataTemplate>
     </datagrid:DataGridTemplateColumn.CellEditingTemplate>
    <datagrid:DataGridTemplateColumn>
    
    Friday, March 26, 2010 2:08 PM
  • Have you succeeded?

    Marc

    Wednesday, March 31, 2010 11:15 AM
  • Not yet Marc, I on the techdays in Belgium for 3 days. I have to try at the end of this week.

    Thanks for asking

    Bart

    Wednesday, March 31, 2010 12:55 PM
  • I don't know about him but I certainly have. Thanks for your guidance Marc!

    Wednesday, April 07, 2010 12:27 PM
  • Hello,

     I try to follow your example. But my Combobox selected item is not match. When I enter the edit mode, the seleted item of my combobox is empty and not the reference to my datarecord. I don't find, what I do wrong. How I've to set the reference between CountryCodes (Combobox) and Manufacturers (Datagrid) to show the right Country as selected value, when I enter the editmode of my Datagrid?

    My Solution is separate in three projects: SVAdmin2010_V4 (SL), SVAdmin2010_V4.Web (Web) with Reference to Project DomainModel (Eintity and DomainService)

    First I've create a class - CountryCodesLookupProvider.cs:

    1    using SVDomainModel;
    2    using System.ServiceModel.DomainServices.Client;
    3   
    4    namespace SVAdmin2010_V4
    5    {
    6        public class CountryCodesLookupProvider
    7        {
    8            public static SVAdminDomainContext ctx = new SVAdminDomainContext();
    9            public EntitySet<CountryCode> CountryCodes
    10           {
    11               get
    12               {
    13                   ctx.Load(ctx.GetCountryCodesQuery(), false);
    14                   return ctx.CountryCodes;
    15               }
    16           }
    17       }
    18   }

    xaml of my navigation page in my Silverlight Business Template:

    1    <navigation:Page xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="SVAdmin2010_V4.Views.Administration"
    2               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    5               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    6               mc:Ignorable="d"
    7               xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
    8               xmlns:local="clr-namespace:SVAdmin2010_V4"
    9               d:DesignWidth="640" d:DesignHeight="480"
    10              Title="Administration Page" xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices" xmlns:my="clr-namespace:SVDomainModel">
    11      
    12       <Grid x:Name="LayoutRoot">
    13           <Grid.Resources>
    14               <local:CountryCodesLookupProvider x:Name="LookupProvider_CountryCodes" x:Key="LookupProvider_CountryCodes" />
    15           </Grid.Resources>
    16           <Grid.ColumnDefinitions>
    17               <ColumnDefinition Width="212*" />
    18               <ColumnDefinition Width="256*" />
    19               <ColumnDefinition Width="172*" />
    20           </Grid.ColumnDefinitions>
    21           <Grid.RowDefinitions>
    22               <RowDefinition Height="131*" />
    23               <RowDefinition Height="121*" />
    24               <RowDefinition Height="228*" />
    25           </Grid.RowDefinitions>
    26           <sdk:DataGrid Name="manuDataGrid" Grid.ColumnSpan="3" Grid.Row="0" Margin="0,0,0,26"
    27                         HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
    28                         AutoGenerateColumns="False"
    29                         >
    30               <sdk:DataGrid.Columns>
    31                   <sdk:DataGridTextColumn Header="Id" Binding="{Binding Manufacturer_Id}" />
    32                   <sdk:DataGridTextColumn Header="Name" Binding="{Binding Manufacturer_Name}" />
    33  
    34                   <sdk:DataGridTemplateColumn Header="Country Code">
    35                       <sdk:DataGridTemplateColumn.CellTemplate >
    36                           <DataTemplate>
    37                               <TextBlock Text="{Binding CountryCode.CountryCode_Name}" />
    38                           </DataTemplate>
    39                       </sdk:DataGridTemplateColumn.CellTemplate>
    40                       <sdk:DataGridTemplateColumn.CellEditingTemplate>
    41                           <DataTemplate>
    42                               <ComboBox x:Name="cboCountryCodes"
    43                                         ItemsSource="{Binding Path=CountryCodes, Source={StaticResource LookupProvider_CountryCodes}}"
    44                                         SelectedValuePath="CountryCode_Id"
    45                                         SelectedValue="{Binding CountryCodes, Mode=TwoWay}"
    46                                         DisplayMemberPath="CountryCode_Name"
    47                                        
    48                                         />
    49                                        
    50                           </DataTemplate>
    51                       </sdk:DataGridTemplateColumn.CellEditingTemplate>
    52                   </sdk:DataGridTemplateColumn>
    53               </sdk:DataGrid.Columns>
    54             
    55           </sdk:DataGrid>
    56           <sdk:DataPager Name="manuDataPager" PageSize="20" Source="{Binding Path=ItemsSource, ElementName=manuDataGrid}"
    57                          Grid.ColumnSpan="3" Grid.Row="0" Margin="0,-12,0,0" Height="26" VerticalAlignment="Bottom"  />
    58       </Grid>
    59   </navigation:Page>

    my codebehind:

     

    1    using System.Windows;
    2    using System.Windows.Controls;
    3    using System.Windows.Navigation;
    4    using SVDomainModel;
    5    using System.Collections.ObjectModel;
    6    
    7    namespace SVAdmin2010_V4.Views
    8    {
    9        public partial class Administration : Page
    10       {
    11           private SVAdminDomainContext _dc = new SVAdminDomainContext();
    12                   
    13           public Administration()
    14           {
    15               InitializeComponent();
    16               Loaded += new RoutedEventHandler(Administration_Loaded);
    17           }
    18   
    19           void Administration_Loaded(object sender, RoutedEventArgs e)
    20           {
    21               _dc = new SVAdminDomainContext();
    22               manuDataGrid.ItemsSource = _dc.Manufacturers;
    23               _dc.Load(_dc.GetManufacturersQuery());
    24           }
    25   
    26           // Executes when the user navigates to this page.
    27           protected override void OnNavigatedTo(NavigationEventArgs e)
    28           {
    29           }        
    30   
    31           public ObservableCollection<CountryCode> Ref_LookupProvider_CountryCodes = new ObservableCollection<CountryCode>();
    32   
    33       }
    34   }
    

     Regards

    Dirk

    Wednesday, May 26, 2010 4:01 AM
  •  

    Thursday, July 15, 2010 9:36 AM
  • Hi,

     

    This is working for me:

           <riaControls:DomainDataSource AutoLoad="True" x:Key="projectsDomainDataSource" QueryName="GetProjectsQuery" DomainContext="{StaticResource contractorDomainContext}" />
               	<sdk:DataGridTemplateColumn x:Name="projectIDColumn" Header="Project" Width="SizeToHeader">
                 <sdk:DataGridTemplateColumn.CellTemplate>
                 <DataTemplate>
                 <TextBlock Text="{Binding Project.Name}"/>
                 </DataTemplate>
                 </sdk:DataGridTemplateColumn.CellTemplate>
                 <sdk:DataGridTemplateColumn.CellEditingTemplate>
                 <DataTemplate>
                 <ComboBox ItemsSource="{Binding Data, Source={StaticResource projectsDomainDataSource}}" DisplayMemberPath="Name" SelectedValue="{Binding ProjectID, Mode=TwoWay}" SelectedValuePath="ProjectID"  />
                 </DataTemplate>
                 </sdk:DataGridTemplateColumn.CellEditingTemplate>
                 </sdk:DataGridTemplateColumn>
     
     
     
    Thursday, July 15, 2010 12:40 PM
  • There are two very important considerations in binding a combobox inside a DataGrid.

    1. DataGrid and DataForm both impose namescopes that are disjunct with the page namescope
    2. CellTemplate and CellEditingTemplate binding modes for SelectedValue must both be TwoWay

    The broken namescope means Binding ElementName cannot resolve page level elements. Declaring your domain data sources as static resources puts them into a global namespace that is accessible with the StaticResource notation. This is a horrible hack but at least it works.

    Why the binding mode of the CellTemplate element has any significance to the behaviour of the element in the CellEditingTemplate is something that perplexes me, but it does. Generally you can simply not bother to set the mode for either template, because most controls are marked up for a default of TwoWay binding. When in doubt, explicitly set Mode=TwoWay for both templates.

    Monday, July 19, 2010 12:50 AM
  • Hi,

    I used your code in my .cs 

    public

    class

    EntitiesLookupProvider

    {

     

    public static myDomainContext Context = new myDomainContext();

     

    public EntitySet<Entity> Entities

    {

    get

    {

    Context.Load(Context.GetOPLookupNotesQuery(),

    false);

     

    return Context.Entities;

    }

    }

    }

    I get a error as "myDomainContext doesnot contain a defenition for Entities" in the line "returnContext.Entities;".

    How I correct this error ?

     

    Thank you in advance.

    Friday, December 17, 2010 5:14 AM
  • I request you to expalin with full code so that Sivlerlight frershers and understand it easily.

     

    Thank you.

    Friday, December 17, 2010 6:54 AM
  • use your actual entitySet's name

    return Context.<yourEntitySet>;

    Thursday, January 13, 2011 8:45 AM
  • Hi Marc,

    I was able to use your code properly and I am very thankful for that.

    I just have one request, can you give me the code to update the lookup resource if the data is updated?

    Thanks,

    kEn

    Thursday, January 13, 2011 8:48 AM
  • I am not sure what you are asking. The lookup resource is retrieved everytime the control is initialized, so it should be fresh.

    Marc

    Thursday, January 13, 2011 9:04 AM
  • Hi Marc,

    Actually my situation is, I have grid with one combo box column (UnitOfMeasurement column) being populated by the lookup provider. When the page is loaded the all the UnitOfMeasurement records are there.

    On the same page, I also have link that shows a childWindow for adding new UnitOfMeasurements. What I wanted to achieve is to have the newly added UnitOfMeasurements record be added on the LookUpProvider or the UnitOfMeasurement combo box on the grid once it is saved.

    Thanks,

    kEn




    Sunday, January 16, 2011 12:49 AM
  • You need to use the callback on the submit of the new UnitOfMeasure to reset the combobox's ItemsSource:

    1. Set the ComboBox.ItemsSource = null
    2. Clear the unitsofmeasurecontext.EntityContainer
    3. Reload the unitsofmeasurecontext
    4. Reset the Combobox.ItemsSource in the LoadOp.Completed event
    Sunday, January 16, 2011 4:41 AM
  • thanks Marc! I finally made it work.

    Thursday, January 27, 2011 8:04 AM
  • Great! Marc
    Friday, January 28, 2011 2:06 PM
  • hello Marc

    i copied your code.but still no success in loading data in comboBox.

    i did not bind dataGrid, i just want to display a bind comboBox in blank datagrid.

    if you can post the complete code in sequence, it will be very helpful.



    Monday, February 14, 2011 7:13 AM
  • I had the same issue I did that in this way

    <UserControl.Resources>
            <riaControls:DomainDataSource x:Key="cmbRoles" AutoLoad="True" d:DesignData="{d:DesignInstance my2:Role, CreateList=true}" Height="0" LoadedData="roleDomainDataSource_LoadedData" Name="roleDomainDataSource" QueryName="GetRolesQuery" Width="0">
                <riaControls:DomainDataSource.DomainContext>
                    <my1:UserRoleContext />
                </riaControls:DomainDataSource.DomainContext>
            </riaControls:DomainDataSource>
        </UserControl.Resources>

    .

    .

    .

    <sdk:DataGridTemplateColumn SortMemberPath="RoleID" CanUserSort="True">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=Role}"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                        <sdk:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <sr:ComboBox Height="23" HorizontalAlignment="Left" SelectedValuePath="RoleID" DisplayMemberPath="Role1" SelectedValue="{Binding Path=RoleID,ElementName=tUserDomainDataSource}" ItemsSource="{Binding Data,Source={StaticResource cmbRoles},Mode=OneWay}" Name="roleComboBox" VerticalAlignment="Top" Width="100">
                                </sr:ComboBox>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellEditingTemplate>
                    </sdk:DataGridTemplateColumn>

    Sunday, February 20, 2011 9:08 AM
  • Hi,

    The EntitiesLookupProvider class is created where in a applikation? In the web-folder or the project folder?

    I created a Presentation Model because I have a main table customer and four other tables which are joined with the customer-table with GUIDs.

    My datagrid is filled with the datas from the PresentationModel.

    Now i will select data from a combobox to change the coloumns. But these datas I need to get from another domainDataSource.

    <riaControls:DomainDataSource x:Name="kundeDomainDataSource" AutoLoad="True" LoadedData="kundeDomainDataSource_LoadedData" QueryName ="GetKunde"> 

    <riaControls:DomainDataSource.DomainContext>

    <domain:KundenlisteContext />

    </riaControls:DomainDataSource.DomainContext>

    Here is the cs-File of my PresentationModel

    

    using

    System;

    using

    System.Collections.Generic;

    using

    System.Linq;

    using

    System.Web;

    using

    System.ServiceModel.DomainServices.Hosting;

    using

    System.ServiceModel.DomainServices.Server;

    using

    ORGSYS_V01.Web.Models;

    namespace

    ORGSYS_V01.Web.Services

    {

    [

    EnableClientAccess

    ]

    public class KundenlisteService :

    DomainService

    {

    private orgsysDotNetEntities context = new orgsysDotNetEntities

    ();

    public IQueryable<KundePresentationModel

    > GetKunde()

    {

    return from t in

    context.tbl_kundenliste.OrderBy(p => p.plz).ThenBy(n => n.tbl_mitarbeiter.nachname)

    select new KundePresentationModel

    ()

    {

    kundenliste_id = t.kundenliste_id,

    adid = t.adid,

    br = t.br,

    handelsorganisation = t.tbl_handelsorganisation.handelsorganisation,

    handelsorgid = t.tbl_handelsorganisation.handelsorganisations_id,

    handelszentrale = t.tbl_handelszentrale.handelszentrale,

    handelzlid = t.tbl_handelszentrale.handelszentrale_id,

    ort = t.ort,

    ortsteil = t.ortsteil,

    plz = t.plz,

    strasse = t.strasse,

    hsnr = t.hsnr,

    nachname = t.tbl_mitarbeiter.nachname,

    vorname = t.tbl_mitarbeiter.vorname,

    marktname = t.tbl_marktname.marktname,

    marktnamezusatz = t.marktnamezusatz,

    zentrale = t.zentrale,

    parken = t.parken,

    privatisiert = t.privatisiert,

    geschlossen = t.geschlossen

    };

    }

    private void MapKunde(tbl_kundenliste kunde, KundePresentationModel

    kundePM)

    {

    kunde.br = kundePM.br;

    kunde.adid = kundePM.adid;

    kunde.ort = kundePM.ort;

    kunde.ortsteil = kundePM.ortsteil;

    kunde.plz = kundePM.plz;

    kunde.strasse = kundePM.strasse;

    kunde.hsnr = kundePM.hsnr;

    kunde.marktnamezusatz = kundePM.marktnamezusatz;

    kunde.zentrale = kundePM.zentrale;

    kunde.parken = kundePM.parken;

    kunde.privatisiert = kundePM.privatisiert;

    kunde.geschlossen = kundePM.geschlossen;

    }

    [

    Update

    ]

    public void UpdateKunde(KundePresentationModel

    kundePM)

    {

    tbl_kundenliste

    kunde = context.tbl_kundenliste.Where(e => e.kundenliste_id == kundePM.kundenliste_id).FirstOrDefault();

    MapKunde(kunde, kundePM);

    context.SaveChanges();

    }

    }

    }

    In my ComboBox I need the datas from

    the table tbl_handelsorganisation

    handelsorganisation = t.tbl_handelsorganisation.handelsorganisation,

    handelsorgid = t.tbl_handelsorganisation.handelsorganisations_id,

    

    Do I need another DataSource? Hope you understand what I mean.

    Thursday, April 28, 2011 9:09 AM
  • I know this is an old thread, but I have this issue and it's very much the same

    I follow the example and it works great even the selected item works the combobox it´s great but The textbock doesn´t show any data it's blank is like it can't see the data context of the staticResource let me show you. the only difference that I can see is that I bind the datagrid in designtime in the xaml, can you help me?

     

    <sdk:DataGrid AutoGenerateColumns="False" Height="117" HorizontalAlignment="Left" 
               ItemsSource="{Binding ElementName=Mpersona_idDomainDataSource, Path=Data}" Margin="12,185,0,0" Name="Mpersona_idDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="739">
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTemplateColumn Header="Country Code">
                        <sdk:DataGridTemplateColumn.CellTemplate >
                            <DataTemplate>
                                <TextBlock Text="{Binding ctipo_id.txt_tipo_id, Mode=TwoWay}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                        <sdk:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="cboCountryCodes"
                                            ItemsSource="{Binding Path=TipoId, Source={StaticResource LookupProvider_CountryCodes}}"
                                            SelectedValuePath="id_tipo_id"
                                            SelectedValue="{Binding id_tipo_id, Mode=TwoWay}"
                                            DisplayMemberPath="txt_tipo_id"/>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellEditingTemplate>
                    </sdk:DataGridTemplateColumn>
                    <sdk:DataGridTextColumn x:Name="Id_tipo_idColumn" Binding="{Binding Path=id_tipo_id}" Header="Id tipo id" Width="SizeToHeader" />
                    <sdk:DataGridTextColumn x:Name="Nro_id_personaColumn" Binding="{Binding Path=nro_id_persona}" Header="Nro id persona" Width="SizeToHeader" />
                    <sdk:DataGridTextColumn x:Name="Txt_id_personaColumn" Binding="{Binding Path=txt_id_persona}" Header="Txt id persona" Width="SizeToHeader" />
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>

     

    Sunday, May 27, 2012 11:42 PM
  • This is simpliest way to add resources through domaindatasource. I try to show Role1 instead of RoleID on the datagrid, but it doesn't work like what i think.

    <sdk:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
    <ComboBox HorizontalAlignment="Left"
    ItemsSource="{Binding Data,Source={StaticResource category},Mode=OneWay}"
    DisplayMemberPath="Role1"
    SelectedValuePath="RoleID"
    SelectedValue="{Binding Path=RoleID}">
    </ComboBox>
    </DataTemplate>
    </sdk:DataGridTemplateColumn.CellTemplate>

    So any idea. thanks.

    Saturday, October 27, 2012 3:52 AM