简体   繁体   English

将DataGridView导出为XML-C#

[英]Export DataGridView to XML - C#

I have a DataGridView in my application with values from a database table. 我的应用程序中有一个DataGridView,其中包含数据库表中的值。 I also have functions that filter the data in the gridview. 我还有过滤网格视图中数据的函数。

I want to know if its possible to save the filtered data in the gridview in an XML file. 我想知道是否有可能将过滤后的数据保存在XML文件的gridview中。

How would I do this? 我该怎么做?

Thanks. 谢谢。

If your datatables datasource is a DataTable then 如果您的数据表数据源是一个DataTable那么

table.DefaultView

contains a subset of your data that reflects the current sorting / filtering 包含反映当前排序/过滤的数据子集

var table = dataGridView1.DataSource as DataTable;
var view = table.DefaultView;

this view can be saved as XML, too 该视图也可以另存为XML

view.ToTable().WriteXml(@"c:\view.xml");

Are you sure that your datagridview is using datatable as the source? 您确定您的datagridview使用datatable作为源吗? It looks like you are using some different object such as dataset as source. 看起来您正在使用一些不同的对象,例如数据集作为源。 Try 尝试

DataSet ds = new DataSet(); DataSet ds = new DataSet();

ds = (DataSet)datagridview.DataSource; ds =(DataSet)datagridview.DataSource;

ds.Tables[0].WriteXml(xml_file, System.Data.XmlWriteMode.IgnoreSchema); ds.Tables [0] .WriteXml(xml_file,System.Data.XmlWriteMode.IgnoreSchema);

您是否尝试过DataTable.WriteXml()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM