如何更新XML数据集Datagrid View中显示的数据?

时间:2019-01-21 23:07:33

标签: c# winforms linq-to-xml

因此,我现在几乎正在尝试更新绑定的数据源XML文件,以便当单击按钮时,项目的数量下降...但是...数量单元的值...从不下降...这就是我所拥有的。

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Windows;
using System.Xml.Linq;

namespace Portal_of_Asura
{
    public partial class ShopForm : Form
    {
        public string ShopName;
        public string ShopKeeperName;
        public string ShopSpecies;
        public List<XmlNode> Items = new List<XmlNode>() { };
        public ShopForm(String ShopName)
        {
            InitializeComponent();

        }
        public DataSet ds = new DataSet();

        private void ShopForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            ds.WriteXml("./ReferenceXMLS/ShopList.xml");


        }

        private void ShopForm_Load_1(object sender, EventArgs e)
        {



            //XElement xelement = XElement.Load("./ReferenceXMLS/ShopList.xml");
            //
            //IEnumerable<XElement> books = xelement.Elements();
            //List<XElement> books2 = new List<XElement> { };
            //foreach(var element in books)
            //{
            //    books2.Add(element);
            //
            //}
            //foreach (var element2 in books2)
            //{
            //    if(element2.Attribute("name").Value == "Tiarga")
            //    {
            //        
            //    }
            //}

            var xml = XDocument.Load(@"./ReferenceXMLS/ShopList.xml");
            var ds = xml.Root.Descendants("Shop")
                    .Single(x => x.Attribute("name").Value == "Tiarga")
                    .Descendants("Item")
                    .Select(x => new {
                        Name = x.Attribute("name").Value,
                        Price = x.Descendants("Price").FirstOrDefault()?.Value,
                        Stats = x.Descendants("Stats").FirstOrDefault()?.Value,
                        Quantity = x.Descendants("Quantity").FirstOrDefault()?.Value,
                    })
                    .ToList();
            dataGridView1.DataSource = ds;


            //Console.WriteLine(ds.Tables["Item"].ToString());


            //dataGridView1.DataSource = ds.Tables["Item"];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var targetRow = dataGridView1.CurrentCell.RowIndex;
            var cellValue = dataGridView1.Rows[targetRow].Cells[3].Value.ToString();
            Console.WriteLine(dataGridView1.Rows[targetRow].Cells[3].Value);
            dataGridView1.Rows[targetRow].Cells[3].Value = int.Parse(cellValue) - 1;
            ds.AcceptChanges();
        }

    }
}

但是在button1_click()上,我希望它更改xml中的值并显示更改...在我将每个xmlNode解析到表(很多代码)之前,我可以这样做。 。但是现在它已绑定...我很难更改它...帮助吗?

0 个答案:

没有答案