簡體   English   中英

將datagridview保存在文件xml中c#

[英]save datagridview in file xml c#

我在將 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);
            }
        }

提前致謝

嘗試這個?

        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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM