如何在Xml中保存DatagridView并将Xml加载到datagridView? [英] How I can save a DatagridView in a Xml and Load A Xml to datagridView?
本文介绍了如何在Xml中保存DatagridView并将Xml加载到datagridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:为了保存
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文件
< dataset>
< student>
< name> Tarasov< / name>
< / student>
< / dataset>
可以访问上述XML文件的示例代码片段
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
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()
Here is my code: For saving
private void Save(DataGridView grid)
{
try
{
xmlfile = @"C:\datagrid.xml";
dataset = (DataSet)InputDataGrid.DataSource;
dataset.WriteXml(xmlfile);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
How I can do this?
解决方案
This is the sample xml file which i have used for testing your scenario
<dataset>
<student>
<name>Tarasov</name>
</student>
</dataset>
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
这篇关于如何在Xml中保存DatagridView并将Xml加载到datagridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文