Export DataGridView to XML - C#
I have a DataGridView in my application with values from a database table. 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.
How would I do this?
Thanks.
If your datatables datasource is a DataTable
then
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
view.ToTable().WriteXml(@"c:\view.xml");
Have you tried DataTable.WriteXml()?
Are you sure that your datagridview is using datatable as the source? It looks like you are using some different object such as dataset as source. Try
DataSet ds = new DataSet();
ds = (DataSet)datagridview.DataSource;
ds.Tables[0].WriteXml(xml_file, System.Data.XmlWriteMode.IgnoreSchema);
精彩评论