通过linq绑定到XML文件后无法编辑我的datagridview [英] Can't edit my datagridview after binding to XML file via linq

查看:62
本文介绍了通过linq绑定到XML文件后无法编辑我的datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接到这里的2个类似示例,我无法真正链接到我的确切案例. Similalr示例1

Links to 2 similar examples on here which I can't really link to my exact case. Similalr example 1

相似示例2

这是填充我的datagridview的代码...

Here is the code that populates my datagridview...

XElement xdoc = XElement.Load(@"C:\xmltest\test.xml");

            var lines = from item in xdoc.Descendants("line")
                        let fields = item.Elements("field")
                        select new
                        {
                            Name = (string)fields
                                .FirstOrDefault(n => (string)n.Attribute("name") == "Name"),
                            Description = (string)fields
                                .FirstOrDefault(n => (string)n.Attribute("name") == "Description"),
                            ExtraDetails = (string)fields
                                .FirstOrDefault(n => (string)n.Attribute("name") == "ExtraDetails"),

                        };


            dataGridView1.DataSource = lines.ToArray();

这工作正常,但是在导入"之后我无法编辑datagridview.我已经严格地设置了datagridview设置以允许编辑等.问题似乎与databind有某种联系.

This works fine but I can't edit the datagridview after the 'import'. I have defiantly set the datagridview settings to allow editing etc. The problem seems to be related to the databind in some way.

推荐答案

问题是您将结果投影到

The problem is that you are projecting the result to anonymous type. The very first line in the documentation link states

匿名类型提供了一种方便的方法,可以将一组只读属性封装到单个对象中,而不必先明确定义一个类型.

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.

希望您注意到只读一词.

如果要获取可编辑的数据,请创建具有读取/写入属性的类,并将查询结果投影到其中.

If you want to get editable data, then create your own class with read/write properties and project the query result into it.

这篇关于通过linq绑定到XML文件后无法编辑我的datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆