的LINQ to XML和的DataGridView [英] LINQ to XML and DataGridView

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

问题描述

您好我想要使用一个DataGridView首次与LINQ。

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

下面就是我想要做的: 我想用它(虽然它没有到DataGridView)来读取并显示XML文件的内容(该位以下工作code),但我希望有窗体上添加行按钮或在于利用了三个文本框和内容,并填充新行的三列中的内容在DataGridView。新行要被附加到现有的数据

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.

和最后我希望有一个保存按钮出口的DataGridView的内容回一个XML文件覆盖/更新现有的XML文件。

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!!

所以这是code我要读XML文件:

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文档:

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>

好,我现在已经改变了我的code到这一点:

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;

这让我从DataGridView中添加和删除行和数据:)

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

但我仍然找不到任何方法来将数据写入回XML无论是从DataGridView中或从bindingSource1:(

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

任何帮助吗? 在bindingSource1.count变化,每次我更改了数据,所以我认为我接近!

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

推荐答案

或许你可以尝试使用保存在会话(因此preserved与回传)的xmlDoc中工作...添加三要素(从文本框)将xmlDoc中......而重新绑定的GridView的xmlDoc中,在每一个回发。

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.

最后,你可以做xmlDoc.save()。

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

有关删除行,你可以使用GridView的事件,dataGridView1_RowDeleted,编辑xmlDoc中(除去节点相对于所选行)。

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

是这样的:

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

这篇关于的LINQ to XML和的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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