无法编辑使用 LINQ 查询结果填充的 DataGridView

发布于 2024-08-22 15:47:25 字数 656 浏览 5 评论 0原文

当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩套路吗 2024-08-29 15:47:25

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

  1. 创建一个空的 WindForm 项目(VS 2008 和 .Net 3.5)
  2. 将 DataGridView 添加到表单。
  3. 连接 CellBeginEdit 和 CellEndEdit。
  4. 将以下代码放入 Form 的构造函数中

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 事件。希望使用上述步骤进行重现能够帮助您找到所面临的问题。

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

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.

紅太極 2024-08-29 15:47:25

由于您使用的是 ToList,您的 DataSource 实际上是一个 List,因此它来自 Linq 查询的事实并不'不要改变任何事情。我怀疑列(而不是 DGV)的 ReadOnly 属性设置为 true...我没有看到网格不可编辑的其他原因

Since you're using ToList, your DataSource is actually a List<MergeEntry>, so the fact that it comes from a Linq query doesn't change anything. I suspect the ReadOnly property of the columns (not the DGV) is set to true... I see no other reason why the grid wouldn't be editable

清醇 2024-08-29 15:47:25

这个解决方案可能效率不高,但对我有用,
我将所有数据(来自 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文