无法编辑使用 LINQ 查询结果填充的 DataGridView
当我使用 linq-to-xml 查询的结果来填充 datagridview 时,我无法编辑 datagridview。我尝试将 datagridview 的只读属性设置为 false,但这没有帮助。我还为 cellBeginEdit 添加了一个事件处理程序,并在那里放置了一个断点,但它没有被命中。知道我做错了什么,或者这不可能吗?
public class MergeEntry
{
public string author { get; set; }
public string message { get; set; }
}
...
var query = from entry in xmlDoc.Descendants("entry")
select new MergeEntry
{
author = entry.Element("author").Value,
message = entry.Element("msg").Value,
}
var queryAsList = query.ToList();
myBindingSource.DataSource = queryAsList;
myDataGridView.DataSource = myBindingSource;
When i use the results of a linq-to-xml query to populate a datagridview, i cannot edit the datagridview. i've tried setting the datagridview's readonly property to false and that doesn't help. I also added an event handler for cellBeginEdit and put a breakpoint there, but it doesn't get hit. Any idea what i'm doing wrong, or if this isn't possible?
public class MergeEntry
{
public string author { get; set; }
public string message { get; set; }
}
...
var query = from entry in xmlDoc.Descendants("entry")
select new MergeEntry
{
author = entry.Element("author").Value,
message = entry.Element("msg").Value,
}
var queryAsList = query.ToList();
myBindingSource.DataSource = queryAsList;
myDataGridView.DataSource = myBindingSource;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,可以绑定到从 Linq-To-Xml 创建的列表。我试图重现你的问题。我执行了以下操作:
运行 WinForm 应用程序时,会显示一个可编辑网格,并且会在应该触发的时候触发 CellBeginEdit 和 CellEndEdit 事件。希望使用上述步骤进行重现能够帮助您找到所面临的问题。
Yes, it is possible to bind to a list created from Linq-To-Xml. I tried to reproduce your problem. I did the following:
When running the WinForm app, an editable grid was displayed and the CellBeginEdit and CellEndEdit events were fired when they should have been. Hopefully trying to reproduce using the above steps will help you locate the issue you are facing.
由于您使用的是
ToList
,您的DataSource
实际上是一个List
,因此它来自 Linq 查询的事实并不'不要改变任何事情。我怀疑列(而不是 DGV)的ReadOnly
属性设置为 true...我没有看到网格不可编辑的其他原因Since you're using
ToList
, yourDataSource
is actually aList<MergeEntry>
, so the fact that it comes from a Linq query doesn't change anything. I suspect theReadOnly
property of the columns (not the DGV) is set to true... I see no other reason why the grid wouldn't be editable这个解决方案可能效率不高,但对我有用,
我将所有数据(来自 LINQ)移动到一个新集合中,并将该新集合作为数据源传递给 gridview。
现在gridview允许我们编辑单元格。
This solution may not efficient but it works for me,
I moved all data (which is from LINQ) into a new collection and passed that new collection as datasource to gridview.
Now gridview allow us to edit cells.