数据绑定问题:DataGridView <=> XDocument(使用 LINQ 到 XML)

发布于 2024-08-30 18:07:44 字数 1438 浏览 7 评论 0 原文

到目前为止,学习 LINQ 非常有趣,但尽管阅读了几本关于该主题的书籍和大量在线资源,我仍然感觉自己完全是个菜鸟。最近,我刚刚了解到,如果我的查询返回匿名类型,我正在填充的 DataGridView 将是只读的(因为显然匿名类型是只读的。)

现在,我正在尝试找出最简单的方法

  1. :将 XML 文件中的数据子集放入 DataGridView,
  2. 允许用户编辑所述数据,
  3. 并将更改后的数据粘贴回 XML 文件中。

到目前为止,我已经弄清楚了步骤 1 和 2:

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();
    }
}

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());

dataGridView1.DataSource = barcodes.ToList();

这对于将我想要的数据从 XML 获取到 DataGridView 中非常有用,以便用户可以操作这些值。

在对我的代码进行逐步跟踪后,我发现对 DataGridView 中的值所做的更改并未绑定到 XDocument 对象,因此不会传播回来。

我们如何处理第 3 步? (将数据返回到 XML)是否可以将 XML 直接绑定到 DataGridView?或者我是否必须编写另一个 LINQ 语句才能将 DGV 中的数据返回到 XDocument?

建议?

Learning LINQ has been a lot of fun so far, but despite reading a couple books and a bunch of online resources on the topic, I still feel like a total n00b. Recently, I just learned that if my query returns an Anonymous type, the DataGridView I'm populating will be ReadOnly (because, apparently Anonymous types are ReadOnly.)

Right now, I'm trying to figure out the easiest way to:

  1. Get a subset of data from an XML file into a DataGridView,
  2. Allow the user to edit said data,
  3. Stick the changed data back into the XML file.

So far I have Steps 1 and 2 figured out:

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();
    }
}

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());

dataGridView1.DataSource = barcodes.ToList();

This works great at getting the data I want from the XML into the DataGridView so that the user has a way to manipulate the values.

Upon doing a Step-thru trace of my code, I'm finding that the changes to the values made in DataGridView are not bound to the XDocument object and as such, do not propagate back.

How do we take care of Step 3? (getting the data back to the XML) Is it possible to Bind the XML directly to the DataGridView? Or do I have to write another LINQ statement to get the data from the DGV back to the XDocument?

Suggstions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

£噩梦荏苒 2024-09-06 18:07:44

所以我认为您遇到的问题是您绑定的对象与 XML 源文档之间没有关系。

您正在做的是创建一堆对象,推入一些字符串和一个浮点数,然后将网格视图绑定到该对象列表。所有对象只知道构造函数中给出了一些数据,但它不知道这些数据来自哪里。当您调用“select new Something()”时,您正在创建一个新对象,该新对象不知道也不关心它是使用 LINQ to XML 创建的...

我能想到的解决该问题的最简单方法是更改容器属性的设置器,以便它们加载 XML,更改它们应该表示的元素,然后再次保存 xml。也许为容器提供对元素或文档的引用会让这变得更容易。

另一种方法是挂钩网格视图事件,以便在编辑行时可以捕获更改并将其写入 XML 文件。

So I think the problem that you have is that there is no relationship between the objects you are binding to and the XML source document.

What you are doing is creating a heap of objects, pushing in some strings and a float, and then binding the grid view to that list of objects. All the objects know is that some data was given in the constructor, it has no knowledge of where that data come from. When you call "select new something()" you are creating a new object, that new object doesn't know or care that it was created using LINQ to XML...

The easiest way I can think of to resolve it would be to change the setter of your container properties so that they load the XML, change the element they are supposed to represent, and then save the xml again. Perhaps giving the Container a reference to the element or document would make this easier.

The other way would be to hook into the grid view events so that when rows are edited you can capture the changes and write them to the XML file.

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