【问题标题】:How to save DataGridView to XML using Linq without dataset如何使用没有数据集的 Linq 将 DataGridView 保存到 XML
【发布时间】:2017-02-04 05:07:57
【问题描述】:

我有一个 7 列的数据网格视图。此 datagridview 没有与之连接的数据集。用户使用dataGridView.Rows.Add命令输入值...

是否可以将这些文件保存为 XML (Linq)?

【问题讨论】:

  • 可能有几种方法可以做到这一点。您不使用数据集是否有任何具体原因?我的意思是你将如何连接到数据库?您可以使用其他技术,例如 EF,但这实际上取决于您真正想要实现的目标。你能提供更多的细节吗?
  • 没有连接到数据库只是一个带有列的网格。我没有得到它和执行此任务的适当组件。
  • 您使用的是 WPF 还是 winforms?在 WPF 中,您始终可以将数据网格绑定到 observablecollection,然后对其进行序列化。 stackoverflow.com/questions/8633398/…
  • 我正在使用 winforms

标签: c# xml linq dataset


【解决方案1】:

另一种解决方案:

void SaveData() {
        XDocument xmlDocument = new XDocument(new XElement("Grid"));

        foreach(DataGridViewRow row in dataGridView1.Rows)
        xmlDocument.Root.Add(
            new XElement("Grid",
                new XAttribute("xxx1", row.Cells[0].Value.ToString()),
                new XAttribute("xxx2", row.Cells[1].Value.ToString()),
                new XAttribute("xxx3", row.Cells[2].Value.ToString()),
                new XAttribute("xxx4", row.Cells[3].Value.ToString()),
                new XAttribute("xxx5", row.Cells[4].Value.ToString()),
                new XAttribute("xxx6", row.Cells[5].Value.ToString()),
                new XAttribute("xxx7", row.Cells[6].Value.ToString())));
        xmlDocument.Save("@Path");
    }

    void LoadData() {
        try {
            XDocument xmlDocument = XDocument.Load("@Path");

            foreach(XElement el in xmlDocument.Root.Elements()) {
                switch(el.Name.LocalName) {
                    case "Grid":
                        dataGridView1.Rows.Add(el.Attribute("xxx1").Value,el.Attribute("xxx2").Value,
                            el.Attribute("xxx3").Value,el.Attribute("xxx4").Value,el.Attribute("xxx5").Value,
                            el.Attribute("xxx6").Value,el.Attribute("xxx7").Value);
                        break;
                }
            }
        } catch(Exception ex) {
            MessageBox.Show(ex.ToString());
        }
    }

【讨论】:

    【解决方案2】:

    我创造了

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            const string FILENAME = @"c:\temp\test.xml";
            public Form1()
            {
                InitializeComponent();
    
                DataTable dt = new DataTable();
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("Age", typeof(int));
    
                dt.Rows.Add(new object[] { "John", 25 });
                dt.Rows.Add(new object[] { "Mary", 26 });
                dt.Rows.Add(new object[] { "Bill", 27 });
                dt.Rows.Add(new object[] { "Beth", 28 });
    
                dataGridView1.DataSource = dt;
    
                //reverse
                DataTable dt2 = new DataTable("NewTable");
                foreach (DataGridViewColumn column in  dataGridView1.Columns)
                {
                    dt2.Columns.Add(column.Name, column.ValueType);
                }
                //don't save last row of dattagridview which is the blank editable row
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    DataGridViewRow row = dataGridView1.Rows[i];
                    DataRow newRow = dt2.Rows.Add();
                    for (int j = 0; j < row.Cells.Count; j++)
                    {
                        newRow[j] = row.Cells[j].Value;
                    }
                }
    
                dt2.WriteXml(FILENAME, XmlWriteMode.WriteSchema);
            }
        }
    }
    

    来自数据表的数据网格视图。然后从 DGV 反向创建数据表并保存到文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多