c#将DataGridView保存到Xml文件 [英] c# Save DataGridView to Xml file

查看:479
本文介绍了c#将DataGridView保存到Xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我保存文件的按钮:

This is my button to save file:

private void metroButton12_Click(object sender, EventArgs e) // save
{
    DataSet ds = (DataSet)dataGridView1.DataSource;
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.Filter = "XML|*.xml";
    if (sfd.ShowDialog() == DialogResult.OK)
    {
        try
        {
            ds.Tables[0].WriteXml(sfd.FileName);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
}

我尝试保护自己的datagridview到XML,但是当我选择文件时,没有任何惊喜。
当我启动控制台时,我看到以下内容:
System.NullReferenceException:对象引用未设置为对象实例。

I try to safe my datagridview to XML but nothing happpend when I choose file. When I start console I see this: System.NullReferenceException: The object reference was not set to an object instance.

我的gridview看起来像:
ID名称
1 Michale
2 Noob

My gridview look like: ID Name 1 Michale 2 Noob

我在这里做什么错了?..我在网上看到很多东西,但是在stackoverflow和其他论坛上找不到任何解决方案。
新手请耐心等待。谢谢!

What am I doing here wrong?.. I saw a lot of thing's on the web but could not find any solutions in stackoverflow and other forums. Please be patient for newbie guys. Thank you!

推荐答案

在我看来,您的问题不在您想的地方。

To me it sounds like your problem is not where you think it is.


启动控制台时,我看到以下消息:System.NullReferenceException:对象引用未设置为对象实例。

when i start console i see this : System.NullReferenceException: The object reference was not set to an object instance.

对我而言,您的意思是您在启动应用程序时收到消息,而不是单击按钮时。如果在单击之前得到错误,则问题出在其他地方,而不是发布的代码段中。这是您当前正在执行的操作的完整且可测试的代码段。

To me you are implying that you get the message when you launch the application, not when you click the button. If you get the error before you click, your problem is elsewhere and not in the code snippet you posted. Here is a complete and testable snippet of what you are currently doing.

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

namespace DataGridViewToXML_43053387
{
    public partial class Form1 : Form
    {
        //DataSet theDataSet;
        public Form1()
        {
            InitializeComponent();
            InsertDgvIntoForm();
            ExportDgvToXML();
        }

        private void InsertDgvIntoForm()
        {
            //create a data set
            DataSet ds = new DataSet();
            //create a data table for the data set
            DataTable dt = new DataTable();
            //create some columns for the datatable
            DataColumn dc = new DataColumn("ItemName");
            DataColumn dc2 = new DataColumn("ItemValue");
            DataColumn dc3 = new DataColumn("Blah");
            DataColumn dc4 = new DataColumn("Bleh");
            //add the columns to the datatable
            dt.Columns.Add(dc);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            dt.Columns.Add(dc4);

            //create 5 rows of irrelevant information
            for (int i = 0; i < 5; i++)
            {
                DataRow dr = dt.NewRow();
                dr["ItemName"] = "Item" + i + "Name";
                dr["ItemValue"] = "Item" + i + "Value";
                dr["Blah"] = "Item" + i + "Blah";
                dr["Bleh"] = "Item" + i + "Bleh";
                dt.Rows.Add(dr);
            }
            //add the datatable to the datasource
            ds.Tables.Add(dt);
            //just because it looks better on my screen
            dataGridView1.AutoSize = true;
            //make this data the datasource of our gridview
            dataGridView1.DataSource = ds.Tables[0];

        }

        private void ExportDgvToXML()
        {
            DataTable dt = (DataTable)dataGridView1.DataSource;
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "XML|*.xml";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    dt.WriteXml(sfd.FileName);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }
    }
}

这篇关于c#将DataGridView保存到Xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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