无法编辑带有LINQ查询结果的DataGridView [英] unable to edit DataGridView populated with results of LINQ query

查看:259
本文介绍了无法编辑带有LINQ查询结果的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用linq-to-xml查询的结果来填充datagridview时,我无法编辑datagridview。我已经尝试将datagridview的readonly属性设置为false并且没有帮助。我还为cellBeginEdit添加了一个事件处理程序,并在其中放置了一个断点,但没有被击中。任何想法我在做错什么,或者这是不可能的?

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;


推荐答案

是的,可以绑定到创建的列表从Linq到Xml。我试图重现你的问题。我执行了以下操作:

Yes, it is possible to bind to a list created from Linq-To-Xml. I tried to reproduce your problem. I did the following:


  1. 创建了一个空WindForm项目(VS 2008和.Net 3.5)


  2. 将CellBeginEdit和CellEndEdit连接起来。

  3. 将以下代码放在Form的构造函数
  4. $ b中$ b
  1. Created an empty WindForm project (VS 2008 and .Net 3.5)
  2. Added a DataGridView to the Form.
  3. Wired the CellBeginEdit and CellEndEdit.
  4. Placed the following code in the Form's constructor







string testXML =
        @"<p><entry>
          <author>TestAuthor1</author>
          <msg>TestMsg1</msg>  
          </entry></p>
        ";

XElement xmlDoc = XElement.Parse(testXML);

var query = from entry in xmlDoc.Descendants("entry")
            select new MergeEntry
            {
                author = entry.Element("author").Value,
                message = entry.Element("msg").Value,
            }; //You were missing the ";" in your post, I am assuming that was a typo.

//I first binded to a List, that worked fine. I then changed it to use a BindingList
//to support two-way binding.
var queryAsList = new BindingList<MergeEntry>(query.ToList());

bindingSource1.DataSource = queryAsList;
dataGridView1.DataSource = bindingSource1;

当运行WinForm应用程序时,显示可编辑的网格,当CellBeginEdit和CellEndEdit事件被触发时本来应该。希望使用上述步骤进行复制将有助于您找到所面临的问题。

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.

这篇关于无法编辑带有LINQ查询结果的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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