从datgridview向XML文件添加新行时出错? [英] Getting error while adding a new row to a XML file from a datgridview?

查看:133
本文介绍了从datgridview向XML文件添加新行时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个datgridview,当用户决定添加一行时,其值在运行时填充。相同的值也会在添加到XML文件后写入XML文件DataGridView。

但是,不是将数据添加到文件中,以前添加的数据也会从XML文件中删除,并且表单也冻结而不在datagridview中添加行。



我创建xml文件的代码是:

Hi,
I have a datgridview whose values are filled at run-time when the user decides to add a row.The same values are also written to a XML file after it is added to the DataGridView.
But instead of adding data to the file, the previously added data also gets deleted from the XML file and the form also freezes without adding row in the datagridview.

My code for creating the xml file is :

private void createXML()
        {
            DataTable dt = new DataTable();
            foreach (DataGridViewColumn col in dataGridView1.Columns)
            {
                dt.Columns.Add(col.HeaderText);
            }
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataRow dRow = dt.NewRow();
                foreach (DataGridViewCell cell in row.Cells)
                {
                    dRow[cell.ColumnIndex] = cell.Value;
                }
                dt.Rows.Add(dRow);
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            ds.WriteXml(@"D:/HostAdded.xml");
        }





请帮助!!!

紧急:)



Please help!!!
its urgent :)

推荐答案

最简单的方法是删除现有的XML文件并重新创建它。否则,您可能会面临XPath学习曲线。仅供我,我自己使用这种方法。



尝试使用Google搜索DataGridView to Xml以获取其他方式。
The absolutely simplest way to do this is to delete the existing XML file and recreate it. Otherwise you could be facing an XPath learning curve. FYI, I use this method myself.

Try Googling "DataGridView to Xml" for other ways.


这篇关于从datgridview向XML文件添加新行时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆