[英]save datagridview in file xml c#
i have issue with save the datagridview data in file xml but i have issue in below我在将 datagridview 数据保存在文件 xml 中时遇到问题,但我在下面遇到了问题
private void WriteXml(string path) {
try
{
//BindingSource bs = (BindingSource)dataGridView1.DataSource;
DataSet ds = new DataSet();
ds = (DataSet)(dataGridView1.DataSource);
ds.WriteXml(path, XmlWriteMode.IgnoreSchema);
}
catch (Exception ex)
{
MessageBox.Show("ERROR\r\n" + ex.Message);
}
}
Thanks in advance提前致谢
Try this?尝试这个?
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("col1");
dt.Columns.Add("col2");
DataRow rs = dt.NewRow();
rs[0] = "test1";
rs[1] = "test1";
dt.Rows.Add(rs);
ds.Tables.Add(dt);
ds.AcceptChanges();
StreamWriter serialWriter;
serialWriter = new StreamWriter(@"C:\1\test1.xml");
XmlSerializer xmlWriter = new XmlSerializer(ds.GetType());
xmlWriter.Serialize(serialWriter, ds);
serialWriter.Close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.