Can’t quite figure out what else I need to do to make this work.
I am trying to add more rows to my dataset and datagridview then output either one to an xml
Ideally I want to save the values in dataset then bind datagridview and when closing the form output the dataset out to an xml file. But for some reason this doesn’t work. It does update changes to current rows but doesn’t add new rows.
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);
}
}
I have noticed that
dsStore.GetChanges()
returns null unless cell has been edited. So I tried removing that if statement but still nothing.
EDIT: I’ve tried to write to an empty xml file to see if it writes at least something, and no errors it goes through like everything is ok, then when i open up test2.xml its blank nothing was written. 🙁
private void mediaplayer_FormClosing(object sender, FormClosingEventArgs e)
{
string path2 = "..//..//..//test2.xml";
dsStore.WriteXml(path2);
}
Ok I got this to work..
First mistake what that I was trying to WriteXml in the LoadSong function which can’t be done because that function is being called from a ForEach Loop.
I then went to my FormClosing and just did it this way:
And last not sure if it’s needed but I set my dataset on top like this:
added the “public” in case it wasn’t keeping the values..Thanks everyone for you help.