Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why it doesn't save changes into datatable from datagridview?

I have binded datagridview with datatable (Growns). My main goal is, that user can work with datagridview (dataGridView1), filling and updating data and when button SAVE is clicked, all data would be saved into datatable, because I need it for further work.

Everything works fine, exept saving data into datatable. What am I doing wrong?

Here is my code:

private void Form2_Load(object sender, EventArgs e) {
        // TODO: This line of code loads data into the 'tekmovalecDataSet.Odrasli' table. You can move, or remove it, as needed.
        this.grownsTableAdapter.Fill(this.competitorDataSet.Odrasli);
    }

private void buttonSave_Click(object sender, EventArgs e) {
        if (EmptySpace())
        {
                CompetitorDataSet.OdrasliRow newGrownsRow = competitorDataSet.Growns.NewGrownsRow();
                newGrownsRow.StN = textStN.Text;
                newGrownsRow.Name = textN.Text;
                newGrownsRow.Surname = textSN.Text;
                newGrownsRow.Club = textC.Text;
                newGrownsRow.YBirth = textYB.Text;
                competitorDataSet.Growns.Rows.Add(OdrasliNova);
                competitorDataSet.Growns.AcceptChanges();

                this.dataGridView1.DataSource = competitorDataSet.Growns;
                this.Validate();
                this.grownsBindingSource.EndEdit();
                if (dataGridView1.BindingContext[competitorDataSet.Growns] != null)
                {
                    dataGridView1.BindingContext[competitorDataSet.Growns].EndCurrentEdit();
                }
                this.grownsTableAdapter.Update(competitorDataSet.Odrasli);
                this.grownsTableAdapter.Adapter.AcceptChangesDuringUpdate = true;
        }
        else
        {
            MessageBox.Show("Fill ALL data about competitor!");
        }
    }

P.S.: When I manually fill datatable, on form open datagridview is filled, so datatable and datagridview are connected I suppose...

P.S.2.: bool EmptySpace works fine.

like image 711
user2528094 Avatar asked Nov 02 '22 18:11

user2528094


1 Answers

When you set this.Update(competitorDataSet.Odrasli); the TableAdapter updates the changes from DataTable (news, deleted, updated rows) to the database.

Since you call competitorDataSet.Growns.AcceptChanges(); before TableAdapter.Update, all changes in the table are already accepted and TableAdapter has nothing to update.

So just remove

competitorDataSet.Growns.AcceptChanges();

Also, if you set this.grownsTableAdapter.Adapter.AcceptChangesDuringUpdate = true before grownsTableAdapter.Update(competitorDataSet.Odrasli);, the changes will be accepted and so you don't need to accept changes yourself (and it seems to me that default value is True so I am not sure this line is required)

like image 167
Chris Avatar answered Nov 08 '22 07:11

Chris