如何将数据从datagridview写入XML

时间:2017-12-07 13:50:15

标签: c# xml winforms datagridview

我在datagridview中添加了很多空行,我想将数据写入xml。我做了一些小的研究,但我很高兴你没有按照我想要的方式去做。

DataTable dt = new DataTable();
dt.TableName = "SD";
dt = dataGridView1.DataSource as DataTable;
dt.WriteXml("SD.xml");

它不起作用或我无法运行它。

其错误消息:

  

System.NullReferenceException:'对象引用未设置为   一个对象的实例。'

1 个答案:

答案 0 :(得分:0)

dt null很可能会在dt = dataGridView1.DataSource as DataTable;中回来System.NullReferenceException: 'Object reference not set to an instance of an object.,因此投掷dataGridView1.DataSource确保您的as DataTable实际上可以施放using System; using System.Data; using System.Drawing; using System.Windows.Forms; namespace DataGridViewToXML_47696565 { public partial class Form1 : Form { DataTable dgvdt = new DataTable(); DataGridView dgv = new DataGridView(); Button btn = new Button(); public Form1() { InitializeComponent(); InitOurStuff(); } private void InitOurStuff() { this.Controls.Add(dgv); dgv.DataSource = dgvdt; dgv.Dock = DockStyle.Top; dgvdt.TableName = "SD"; dgvdt.Columns.Add(); for (int i = 0; i < 10; i++) { dgvdt.Rows.Add($"row {i}"); } this.Controls.Add(btn); btn.Location = new Point(5, dgv.Location.Y + dgv.Height + 5); btn.Text = "Click me"; btn.Click += Btn_Click; } private void Btn_Click(object sender, EventArgs e) { dgvdt.WriteXml(@"c:\temp\mydtxml.xml"); } } }

这是一个完整的片段。

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace DataGridViewToXML_47696565
{
    public partial class Form2 : Form
    {
        BindingList<dgventry> dgvdata = new BindingList<dgventry>();
        DataGridView dgv = new DataGridView();
        Button btn = new Button();


        public Form2()
        {
            InitializeComponent();
            initOurStuff();
        }

        private void initOurStuff()
        {
            dgv.Dock = DockStyle.Top;
            dgv.DataSource = dgvdata;
            this.Controls.Add(dgv);

            for (int i = 0; i < 10; i++)
            {
                dgvdata.Add(new dgventry { col1 = $"col1 row{i}", col2 = $"col2 row{i}", col3 = $"col3 row{i}" });
            }

            this.Controls.Add(btn);
            btn.Location = new Point(5, dgv.Location.Y + dgv.Height + 5);
            btn.Text = "Click me";
            btn.Click += Btn_Click; ;
        }

        private void Btn_Click(object sender, EventArgs e)
        {


            /*The DataSource connected to the DGV is not already a DataTable,
             so I'll traverse the DataGridView and create my own DataTable out of it*/
            DataTable dt = new DataTable();//create the data table
            dt.TableName = "SD";//give it a name
            //create the appropriate number of columns
            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                dt.Columns.Add(dgv.Columns[i].HeaderText);
            }

            //loop through each row of the DataGridView
            foreach (DataGridViewRow currentRow in dgv.Rows)
            {
                dt.Rows.Add();
                int runningCount = 0;
                //loop trough each column of the row
                foreach (DataGridViewCell item in currentRow.Cells)
                {
                    dt.Rows[dt.Rows.Count - 1][runningCount] = item.FormattedValue;
                    runningCount++;
                }
            }

            if (dt != null)
            {
                dt.WriteXml(@"c:\temp\mynewxml.xml");
            }

        }
    }


    public class dgventry
    {
        public string col1 { get; set; }
        public string col2 { get; set; }
        public string col3 { get; set; }
    }
}

这是我的DataSource还不是DataTable的另一个代码段。

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace DataGridViewToXML_47696565
{
    public partial class Form3 : Form
    {

        DataGridView dgv = new DataGridView();
        Button btn = new Button();

        public Form3()
        {
            InitializeComponent();
            initOurStuff();
        }

        private void initOurStuff()
        {
            dgv.Dock = DockStyle.Top;
            this.Controls.Add(dgv);
            for (int i = 0; i < 10; i++)
            {
                DataGridViewColumn newcol = new DataGridViewTextBoxColumn();
                newcol.Name = $"col{i}";
                dgv.Columns.Add(newcol);
            }

            this.Controls.Add(btn);
            btn.Location = new Point(5, dgv.Location.Y + dgv.Height + 5);
            btn.Text = "Click me";
            btn.Click += Btn_Click; ;
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();//create the data table
            dt.TableName = "SD";//give it a name
            //create the appropriate number of columns
            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                dt.Columns.Add(dgv.Columns[i].HeaderText);
            }

            //loop through each row of the DataGridView
            foreach (DataGridViewRow currentRow in dgv.Rows)
            {
                dt.Rows.Add();
                int runningCount = 0;
                //loop trough each column of the row
                foreach (DataGridViewCell item in currentRow.Cells)
                {
                    dt.Rows[dt.Rows.Count - 1][runningCount] = item.FormattedValue;
                    runningCount++;
                }
            }

            if (dt != null)
            {
                dt.WriteXml(@"c:\temp\mynewxml.xml");
            }
        }
    }
}

这是另一个代码段。在这一个中,DataGridView开始为空。用户必须手动输入数据。

MPMediaItemPropertyAssetURL