如何将 DatagridView 保存在 Xml 中并将 Xml 加载到 datagridView? [英] How I can save a DatagridView in a Xml and Load A Xml to datagridView?

查看:40
本文介绍了如何将 DatagridView 保存在 Xml 中并将 Xml 加载到 datagridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想将数据从 datagridview 保存并加载到 xml.我的想法是,我可以将我的 datagridview 保存到一个 xml 中,如何 this -> "[date]_[name].xml" 稍后我可以加载这些数据.对于这两个操作,我想使用两种方法 --> Save() 和 Load()

Hi I want to save and load data from a datagridview to a xml. My idea is that I can save my datagridview to a xml how this -> "[date]_[name].xml" and later I can load this data. For this two operations I want to use two methods --> Save() and Load()

这是我的保存代码:

private void Save(DataGridView grid) 
{
    try
    {
        xmlfile = @"C:datagrid.xml";
        dataset = (DataSet)InputDataGrid.DataSource;
        dataset.WriteXml(xmlfile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

我该怎么做?

推荐答案

这是我用来测试你的场景的示例 xml 文件:

This is the sample xml file which I have used for testing your scenario:

<dataset>
  <student>
    <name>Tarasov</name>
  </student>
</dataset>

可以访问上述 XML 文件的示例代码片段:

The sample code snippet which could access the above mentioned XML file:

private void Load()
{
    string path = @"C:dataset.xml";
    DataSet ds = new DataSet();
    ds.ReadXml(path);
    InputDataGrid.DataSource = ds;
    InputDataGrid.DataMember = "student";
}

private void Save()
{
    string path = @"C:dataset.xml";
    DataSet ds = (DataSet) InputDataGrid.DataSource;
    ds.WriteXml(path);
}

--SJ

这篇关于如何将 DatagridView 保存在 Xml 中并将 Xml 加载到 datagridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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