【发布时间】:2012-01-20 11:09:18
【问题描述】:
无法完全弄清楚我还需要做什么才能完成这项工作。 我正在尝试向我的数据集和 datagridview 添加更多行,然后将任一行输出到 xml 理想情况下,我想将值保存在数据集中,然后绑定 datagridview,并在关闭表单时将数据集输出到 xml 文件。但由于某种原因,这不起作用。它会更新对当前行的更改,但不会添加新行。
public void LoadSongInfo(string filename)
{
TagLib.File tagFile = TagLib.File.Create(filename);
string artist = tagFile.Tag.FirstAlbumArtist;
string album = tagFile.Tag.Album;
string title = tagFile.Tag.Title;
DataRow newtrack = dsStore.Tables["Track"].NewRow();
newtrack["Id"] = "5";
newtrack["Artist"] = artist;
newtrack["Album"] = album;
newtrack["Filepath"] = filename;
newtrack["Title"] = title;
dsStore.Tables["Track"].Rows.Add(newtrack);
dsStore.Tables["Track"].AcceptChanges();
dataGridView1.DataMember = "Track";
dataGridView1.DataSource = dsStore;
}
private void mediaplayer_FormClosing(object sender, FormClosingEventArgs e)
{
string path = "..//..//..//temp.xml";
dataGridView1.EndEdit();
if (dsStore.GetChanges() != null)
{
dsStore.WriteXml(path);
}
}
我注意到了
dsStore.GetChanges()
除非单元格已被编辑,否则返回 null。所以我尝试删除该 if 语句,但仍然没有。
编辑:我尝试写入一个空的 xml 文件以查看它是否至少写入了一些内容,并且没有任何错误,就像一切正常一样,然后当我打开 test2.xml 时,它的空白没有写入任何内容。 :(
private void mediaplayer_FormClosing(object sender, FormClosingEventArgs e)
{
string path2 = "..//..//..//test2.xml";
dsStore.WriteXml(path2);
}
【问题讨论】:
-
好的,我尝试创建一个新数据集并传递第一个数据集的值。如果我不向数据集添加任何新行,它会将值传递给一个空的 xml 文件,但如果我添加行,它不会传递任何东西。
标签: c# xml winforms datagridview dataset