Skip to content

Datagridview DataSource Edit row problem

Jim

Asked Jul 5, 2008, 1:14 AM EDT3 answers
Share
LinkedInX
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

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Anybody else seen this problem?
    0
  • 0

    Hello Jim

    You are not alone ;-) I'm struggling with exactly the same sh... but in VB.NET. New entries in the new line are not saved. While debugging, I found something interesting: when hitting the save button, the DataGridView contains already eg. 5 rows (a new empty line already added) whereas the bound dataSet only contains the old 3 rows. Also a myDataSet.AcceptChanges() does not help.

    Have you found a solution in the meantime?

    Cheers, Oliver

    Breaking News!!!!! I just found a workaround!

    You just have to set the cursor to another field in the datagridview an everything works fine. So add something like the following vb.net command to the beginning of your save-button-click handler and it should work:

    myDataGridView.CurrentCell = myDataGridView.FirstDisplayedCell

    Cheers, Oliver

    0
  • 0
    Thanks Oliver. Your the man!

    I put this code in to ensure everything has got saved

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

    Thanks.
    0