English 中文(简体)
将 WPF 数据Grid 绑到 LINQ 查询( 实体框架)
原标题:Bind WPF DataGrid to LINQ Query (Entity Framework)

这必须非常简单, 但我似乎漏掉了一些东西。 我搜索了几个小时, 没有找到任何能解决我问题的东西。 问题是, 虽然我可以将我的 LINQ 查询指派给 WPF DataGrid, 但当我试图编辑 DataGrid S 的某个值时, 我得到的错误如下:

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)

数据仪的XAML 看起来像这个:

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

我正在做一个 LINQ 查询, 在所选的 ComboBox 变换事件 中进行这样的 LINQ 查询 :

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;

}

我使用“POCO实体 ”, 如果有人觉得有比较容易的方法来完成这个任务的话。 我真的觉得我漏掉了一些非常基本和显而易见的东西, 如果有人能帮我指出这一点的话! :-)

非常感谢。

最佳回答

我还没有测试过这个,但我敢肯定你的问题是 因为你从查询中又返回了一种匿名类型。试着把它换成

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

在哪里定义 MyRealType 。

问题回答

部分多亏了菲尔,我现在有了一种可行的技术, 包括一种可观察的集束和一种新的持有者类型:

    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;
    }

谢谢!





相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签