Jim

Jim

  • NA
  • 8
  • 0

Datagridview DataSource Edit row problem

Jul 5 2008 5:14 AM
Hello, I have been working on thsi problem for some time now and can not find a solution.

I have a datagridview bound to a datatable via a binding source. I then save the datatable to a XML file. This is all okay.

But the problem comes when the last row of the datagridview has a new bit of data added. I then hit the save option in the menu bar. and then open the same file. the last cell entered is not saved.

I have put in a custom commitDataTable changes, which half works, but not for the newly added row.

To help debug i have add another datagridview2, this is bound to the same datatable, and reflexting the data to be saved. This shows the newly added data. But its still not saved.

Here is my code:



private void Form1_Load(object sender, EventArgs e)
        {    
           
            dt.Columns.Add("id", typeof(int));
            dt.Columns.Add("dateT", typeof(int));

            dt.AcceptChanges();

            bs.DataSource = dt.DefaultView;

            //bind datatable to dgv
            dataGridView1.DataSource = bs.DataSource;
            dataGridView2.DataSource = bs.DataSource;           
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            commitDataTableChanges();
            dt.WriteXml(@"C:\test.xml", XmlWriteMode.WriteSchema);
        }


        private void commitDataTableChanges()
        {           
            dataGridView1.EndEdit();           
            bs.EndEdit();           
            dataGridView2.Refresh();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dt.Clear();
            dt.ReadXml(@"C:\test.xml");
        }



Here is the complete project - Project



To replicate the problem:

Enter a number in the left data gridview R1 C1
File->Save
File->Open
Select R2 C1 ->Enter a number in here
File->Save (this should then copy the data over to the right datagridview and save the data)
The two datagridviews should be the same now.
Then click File->Open

The last number entered was not saved.

It must be something am doing wrong. Thanks in advance.

Jim



Answers (3)