3

XML

<?xml version="1.0" encoding="utf-8"?>
<TrackCollection>
  <Tracks>
      <Id>1</Id>
      <Artist>Artist 1</Artist>
      <Album>His album</Album>
      <Filepath>C://music//song.mp3</Filepath>
      <Id>2</Id>
      <Artist>Artist 1</Artist>
      <Album>His album</Album>
      <Filepath>C://music//song2.mp3</Filepath>
      <Id>3</Id>
      <Artist>Artist 1</Artist>
      <Album>His album</Album>
      <Filepath>C://music//song2.mp3</Filepath>

  </Tracks>
</TrackCollection>

 DataSet dsStore = new DataSet();
 DataTable dt = new DataTable();
 public void loadXmlTracks()
    {
        //TrackCollection tracks = null;
        string path = "..//..//..//test.xml";

        //XmlSerializer serializer = new XmlSerializer(typeof(TrackCollection));

        //StreamReader reader = new StreamReader(path);
        //tracks = (TrackCollection)serializer.Deserialize(reader);
        //reader.Close();
        dsStore.ReadXml(path);
        dt = dsStore.Tables["Track"];

        // finally bind the data to the grid
        LoadGrid(dt);


    }

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    int i;
    int j;

    i = e.RowIndex;
    j = e.ColumnIndex;

    string id = dataGridView1.Rows[i].Cells[0].Value.ToString();
    string value = dataGridView1.Rows[i].Cells[j].Value.ToString();

    DataRow[] row = dt.Select("Id=" + i, "Id");
    foreach (DataRow r in row)
    {
        r[j] = value;
        r.AcceptChanges();
    }

public void LoadGrid(DataTable dt)
    {
        dataGridView1.DataSource = null;
        dataGridView1.DataSource = dt;
    }

EDIT: here's my xml file which is being loaded then I take that and insert into a datatable, then I use that datatable to load my datagridview. What I would like to accomplish is if a user edits a cell in the datagridview to update that row in my datatable so when i close the application it saves back to the XML file.

What I am not sure and can't figure out is how do I save that change back to my Datatable and is it correct to do a Loop if I have the ID of the row that needs to be changed?

I tried RowFilter and that made the change but when I reloaded my datagridview it only show the row I changed..

Andres
  • 2,013
  • 6
  • 42
  • 67

1 Answers1

1

in your code I see some note:

  1. when you call "AcceptChanges" you chage rowstatus of row change from Changed,added,... to unchanged so upadte command of "dataadaptor" cannot find the changes record in your "datatable" and your changes is not apply in the database.
  2. if you know your Id and the ID is primery key in your datatable then you can use "dt.Rows.Find(i)" and it give you the datarow with id that you want

but if your data gridview is bind to your datatable it's not necessary to do such thing when you change one cell these changes will apply to your datatable,the only thing is, when you change one cell the change will not apply to your datatable until you move from one row to other.

EDIT: Delete function "dataGridView1_CellEndEdit" and add the form closing event hanlder like this:

private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
               string path = "..//..//..//test.xml";
               dataGridView1.EndEdit();
               if(dsStore.GetChanges()!=null)
               {
                   dsStore.WriteXml(path);
               }


}
Andres
  • 2,013
  • 6
  • 42
  • 67
MRM
  • 435
  • 2
  • 10
  • the AcceptChanges is just something I was trying, anything in that code can be changed if the end result is what I need. The reason I want to change the datatable when changing a cell value in my grid is because whenever the application is closed it will update the xml it read when it opened. So i have to always be checking the datatable – Andres Dec 12 '11 at 20:10