WPF DataGrid to LINQ Query Binding (Entity Framework)

ef-code-first entity-framework linq wpf-controls

Question

I must be missing something since this must be pretty easy. I looked around for a while but couldn't find anything that might fix my issue. The problem is that, while being able to apply my LINQ query to a WPF DataGrid, I see the following error when attempting to update one of the DataGrid's values:

System.InvalidOperationException was unhandled Message='EditItem' is not allowed for this view. Source=PresentationFramework StackTrace: at System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item) at System.Windows.Controls.DataGrid.EditRowItem(Object rowItem) at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e) at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(Object sender, ExecutedRoutedEventArgs e) at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)

My DataGrid's XAML looks like this:

<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="565" HorizontalAlignment="Left" Margin="6,92,0,0" Name="translatedStringsDataGrid1" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="602">
    <DataGrid.Columns>
    <DataGridTextColumn x:Name="stringsIDColumn2" Binding="{Binding Path=StringsID}" Header="Strings Name" Width="SizeToHeader" />
    <DataGridTextColumn x:Name="translatedStringsValueColumn1" Binding="{Binding Path=TranslatedStringsValue}" Header="Translated Strings Value" Width="SizeToHeader" />
    </DataGrid.Columns>
</DataGrid>

In the SelectedChange event of a ComboBox, I am doing the following LINQ query:

private void cbSelectLang_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

    var query = from o in _context.TranslatedStrings.Local 
                where o.LanguagesID == cbSelectLang.SelectedIndex + 1
                join r in _context.Strings.Local on o.StringsID equals r.StringsID into SubSet2

                from s in SubSet2.DefaultIfEmpty()

                select new { StringsID = s.StringsName, TranslatedStringsValue = o.TranslatedStringsValue };

    this.translatedStringsDataGrid1.ItemsSource = query;

}

If anybody believes there is a simpler method to do this, I'm utilizing "POCO entities." If somebody could be so kind as to point it out to me, I truly do feel like I'm missing something really simple and obvious.:-)

Thank you.

1
0
5/21/2012 3:41:45 PM

Accepted Answer

Although I haven't tried it, I'm very certain that the reason for your difficulty is because your query is returning an anonymous type. Consider updating it to

...
from s in SubSet2.DefaultIfEmpty()
select new MyRealType 
{ 
    StringsID = s.StringsName, 
    TranslatedStringsValue = o.TranslatedStringsValue 
};

where MyRealType must be defined.

1
5/21/2012 5:12:13 PM

Popular Answer

In part because of Phil I finally have a viable method that uses a new holder type and an ObservableCollection:

    private class JoinClass
    {
        public string StringsID { get; set; }
        public string TranslatedStringsValue { get; set; }
    }

    private void cbSelectLang_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ObservableCollection<JoinClass> collection = new ObservableCollection<JoinClass>();

        var query = from o in _context.TranslatedStrings.Local
                                where o.LanguagesID == cbSelectLang.SelectedIndex + 1
                                join r in _context.Strings.Local on o.StringsID equals r.StringsID into SubSet
                                from s in SubSet.DefaultIfEmpty()
                                select new JoinClass { StringsID = s.StringsName, TranslatedStringsValue = o.TranslatedStringsValue };

        foreach (var item in query)
        {
            collection.Add(item);
        }

        this.translatedStringsDataGrid1.ItemsSource = collection;
    }

I'm grateful.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow