LINQ-to-XML 到 DataGridView:无法编辑字段 -- 如何修复?
我目前正在做 LINQ-to-XML 并用我的查询填充 DataGridView 。我遇到的问题是,一旦加载到 DataGridView 中,这些值似乎是不可编辑的(只读)。这是我的代码:
var barcodes = (from src in xmldoc.Descendants("Container")
where src.Descendants().Count() > 0
select
new
{
Id = (string)src.Element("Id"),
Barcode = (string)src.Element("Barcode"),
Quantity = float.Parse((string)src.Element("Quantity").Attribute("value"))
}).Distinct();
dataGridView1.DataSource = barcodes.ToList();
我在某处读到“当您使用匿名类型时,DataGridView 将处于只读模式”。但我找不到解释原因或到底该怎么做。
有什么想法吗?
编辑 - 这是我想出的答案...
因此我添加了一个“Container”类(使用 Get 和 Set <-- 非常重要!)以避免匿名类型成为只读问题:
public class Container
{
public string Id { get; set; }
public string Barcode { get; set; }
public float Quantity { get; set; }
}
// For use with the Distinct() operator
public class ContainerComparer : IEqualityComparer<Container>
{
public bool Equals(Container x, Container y)
{
return x.Id == y.Id;
}
public int GetHashCode(Container obj)
{
return obj.Id.GetHashCode();
}
}
并将 LINQ 语句更改为:
var barcodes = (from src in xmldoc.Descendants("Container")
where src.Descendants().Count() > 0
select
new Container
{
Id = (string)src.Element("Id"),
Barcode = (string)src.Element("Barcode"),
Quantity = float.Parse((string)src.Element("Quantity").Attribute("value"))
}).Distinct(new ContainerComparer());
就是这样!感谢您的帮助,格伦!
I am currently doing LINQ-to-XML and populating a DataGridView with my query just fine. The trouble I am running into is that once loaded into the DataGridView, the values appear to be Un-editable (ReadOnly). Here's my code:
var barcodes = (from src in xmldoc.Descendants("Container")
where src.Descendants().Count() > 0
select
new
{
Id = (string)src.Element("Id"),
Barcode = (string)src.Element("Barcode"),
Quantity = float.Parse((string)src.Element("Quantity").Attribute("value"))
}).Distinct();
dataGridView1.DataSource = barcodes.ToList();
I read somewhere that the "DataGridView will be in ReadOnly mode when you use Anonymous types." But I couldn't find an explanation why or exactly what to do about it.
Any ideas?
EDIT -- Here's the answer I came up with...
So I added a "Container" class (with Get and Set <-- very important!) to avoid the Anonymous types being ReadOnly issue:
public class Container
{
public string Id { get; set; }
public string Barcode { get; set; }
public float Quantity { get; set; }
}
// For use with the Distinct() operator
public class ContainerComparer : IEqualityComparer<Container>
{
public bool Equals(Container x, Container y)
{
return x.Id == y.Id;
}
public int GetHashCode(Container obj)
{
return obj.Id.GetHashCode();
}
}
and changed the LINQ statement to:
var barcodes = (from src in xmldoc.Descendants("Container")
where src.Descendants().Count() > 0
select
new Container
{
Id = (string)src.Element("Id"),
Barcode = (string)src.Element("Barcode"),
Quantity = float.Parse((string)src.Element("Quantity").Attribute("value"))
}).Distinct(new ContainerComparer());
And that's it! Thanks for the help, Glenn!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绑定到匿名类型时数据网格视图处于只读模式的原因是匿名类型是 只读。如果将视图绑定到仅具有只读属性的对象列表,您将获得相同的行为。
我所知道的唯一解决方案是为可编辑的数据创建一个容器。具有定义 get 和 set 属性的类将为您提供所需的内容。
The reason that the data grid view is in ReadOnly mode when binding to anonymous types is that anonymous types are ReadOnly. You will get the same behaviour if you bind the view to a list of objects with only read only properties.
The only solution that I know of is to create a container for the data that is editable. A class with properties defining a get and set will give you what you are after.
这可能是因为 C# 3 中的限制 - 您不能使用匿名类型作为方法的返回类型。例如,请参阅此
It's probably because of limitation in C# 3 - you can't use anonymous types as return types from methods. See for example this