LINQ to XML 和 DataGridView

发布于 2024-09-28 18:12:13 字数 1844 浏览 1 评论 0原文

您好,我第一次尝试使用 DataGridView 和 LINQ。

这就是我想做的: 我想使用它(尽管它不必使用 DataGridView )来读取和显示 XML 文件的内容(这部分是下面的工作代码),但我想在表单或 DataGridView 中添加一个添加行按钮它获取三个文本框的内容并填充新行的三列的内容。新行将附加到现有数据中。

接下来我想添加一个删除按钮来删除当前选定的行。

最后,我想要一个保存按钮,将 DataGridView 的内容导出回 XML 文件,覆盖/更新现有的 XML 文件。

所以我坚持添加额外的数据!但由于我目前不知道删除或保存,我想我会一次性问所有!

所以这是我必须读取 xml 文件的代码:

XDocument xmlDoc = XDocument.Load(@"queues.xml");
var q = from c in xmlDoc.Root.Descendants("Queue")
        select new
        {
            QueueNumber = c.Element("Number").Value,
            QueueName = c.Element("Name").Value,
            QueuePCC = c.Element("QueueTag").Value
        };

dataGridView1.DataSource = q.ToList();

XML 文档:

<?xml version="1.0" encoding="utf-8" ?>
<Queues>
  <Queue>
    <Number>
      001
    </Number>
    <Name>
      mytest
    </Name>
    <QueueTag>
      xyz
    </QueueTag>
  </Queue>
  <Queue>
    <Number>
      002
    </Number>
    <Name>
      Adi2
    </Name>
    <QueueTag>
      ABCD
    </QueueTag>
  </Queue>
</Queues>

好的我现在已将代码更改为:

XDocument xmlDoc = XDocument.Load(@"queues.xml");
var q = from c in xmlDoc.Root.Descendants("Queue")
        select new Queue
        {
            Number = c.Element("Number").Value,
            Name = c.Element("Name").Value,
            QueueTag= c.Element("QueueTag").Value
        };

var queryAsList = new BindingList<Queue>(q.ToList());

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

这允许我从 dataGridView 添加和删除行和数据:)

但我仍然找不到编写的方法将数据从 dataGridView 或 BindingSource1 返回到 XML :(

有什么帮助吗? 每次我更改数据时,绑定源1.count都会更改,所以我认为我已经很接近了!

Hi I’m trying to use a DataGridView for the first time and with LINQ.

Here’s what I’m trying to do:
I want to use it (although it doesn’t have to the DataGridView ) to read and display the contents of an XML file (this bit is working code below) but I want to have an add row button on the form or in the DataGridView that takes the contents of three textboxes and and populates the contents of three columns of the new row. The new row is to be appended to the existing data.

Next I would like to add a Delete button to delete the currently selected row.

And lastly I want a save button that exports the contents of the DataGridView back to an XML file overwriting/updating the existing XML file.

So I stuck at adding the additional data! but as I don't currently have a clue as to the delete or save either I thought I would ask all in one go!!

So this is the code I have to read the xml file:

XDocument xmlDoc = XDocument.Load(@"queues.xml");
var q = from c in xmlDoc.Root.Descendants("Queue")
        select new
        {
            QueueNumber = c.Element("Number").Value,
            QueueName = c.Element("Name").Value,
            QueuePCC = c.Element("QueueTag").Value
        };

dataGridView1.DataSource = q.ToList();

XML document:

<?xml version="1.0" encoding="utf-8" ?>
<Queues>
  <Queue>
    <Number>
      001
    </Number>
    <Name>
      mytest
    </Name>
    <QueueTag>
      xyz
    </QueueTag>
  </Queue>
  <Queue>
    <Number>
      002
    </Number>
    <Name>
      Adi2
    </Name>
    <QueueTag>
      ABCD
    </QueueTag>
  </Queue>
</Queues>

ok I have now changed my code to this:

XDocument xmlDoc = XDocument.Load(@"queues.xml");
var q = from c in xmlDoc.Root.Descendants("Queue")
        select new Queue
        {
            Number = c.Element("Number").Value,
            Name = c.Element("Name").Value,
            QueueTag= c.Element("QueueTag").Value
        };

var queryAsList = new BindingList<Queue>(q.ToList());

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

This allows me to add and delete rows and data from the dataGridView :)

But I can still find no way to writing the data back to XML either from the dataGridView or from the bindingSource1 :(

Any help please?
the bindingSource1.count changes each time I make a change to the data so I think I'm close!

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

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

发布评论

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

评论(1

独闯女儿国 2024-10-05 18:12:13

也许您可以尝试使用会话中保存的 xmlDoc(因此与回发一起保留)...将三个元素(来自文本框)添加到 XmlDoc...并在每次回发时将 gridview 重新绑定到 xmlDoc。

最后,您可以执行 xmlDoc.save()。

要删除行,您可以使用 GridView 事件 dataGridView1_RowDeleted 来编辑 xmlDoc(以删除与所选行相关的节点)。

像这样的东西:

protected void ButtonLoadGridView_Click(object sender, EventArgs e)
{
    XDocument xmlDoc = XDocument.Load(@"f:\queues.xml");
    var q = from c in xmlDoc.Root.Descendants("Queue")
            select new
            {
                QueueNumber = c.Element("Number").Value,
                QueueName = c.Element("Name").Value,
                QueuePCC = c.Element("QueueTag").Value
            };
    dataGridView1.DataSource = q.ToList();
    Session.Add("xmlDoc",xmlDoc);        
}

public void dataGridView1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
    // get some node information:

    int rowIndex = e.RowIndex;            
    string someNodeInfo = dataGridView1.Rows[rowIndex].Cells[0].Text;            

    // then edit xml :
    XmlDocument xmlDoc = (XmlDocument) Session["xmlDoc"];
    xmlDoc.ChildNodes.Item(rowIndex).RemoveAll();
    Session.Add("xmlDoc", xmlDoc);
}


protected void ButtonSave_Click(object sender, EventArgs e)
{
    XmlDocument xmlDoc = (XmlDocument)Session["xmlDoc"];
    xmlDoc.Save(@"f:\queues2.xml");
}

Perhaps you may try working with the xmlDoc saved in the session (thus preserved with the postbacks)... Add the three elements (from the textboxes) to the XmlDoc... And rebind the gridview to the xmlDoc at each postback.

At the end, you could do xmlDoc.save().

For deleting rows, you could use the GridView event, dataGridView1_RowDeleted, to edit the xmlDoc (to remove the node relative to the selected row).

Something like:

protected void ButtonLoadGridView_Click(object sender, EventArgs e)
{
    XDocument xmlDoc = XDocument.Load(@"f:\queues.xml");
    var q = from c in xmlDoc.Root.Descendants("Queue")
            select new
            {
                QueueNumber = c.Element("Number").Value,
                QueueName = c.Element("Name").Value,
                QueuePCC = c.Element("QueueTag").Value
            };
    dataGridView1.DataSource = q.ToList();
    Session.Add("xmlDoc",xmlDoc);        
}

public void dataGridView1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
    // get some node information:

    int rowIndex = e.RowIndex;            
    string someNodeInfo = dataGridView1.Rows[rowIndex].Cells[0].Text;            

    // then edit xml :
    XmlDocument xmlDoc = (XmlDocument) Session["xmlDoc"];
    xmlDoc.ChildNodes.Item(rowIndex).RemoveAll();
    Session.Add("xmlDoc", xmlDoc);
}


protected void ButtonSave_Click(object sender, EventArgs e)
{
    XmlDocument xmlDoc = (XmlDocument)Session["xmlDoc"];
    xmlDoc.Save(@"f:\queues2.xml");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文