5

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.

Chris
  • 8,527
  • 10
  • 34
  • 51
user2528094
  • 53
  • 1
  • 1
  • 6

2 Answers2

2

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)

Chris
  • 8,527
  • 10
  • 34
  • 51
  • 1
    Thanks! It works now =) It seemed that I wanna be on safe side and so I was adding to much code while I didn't understand it. So, thanks for explanation and solution =) – user2528094 Jul 24 '13 at 09:47
0

You are not editing the data with the datagridview, you are changing the dataset using the textboxes, I think this is your example with the manual fill...

I will presume that the code you want to use to update the database begins at this line:

this.dataGridView1.DataSource = competitorDataSet.Growns;

I suspect your problem is in the following code block (explanations in code comments):

//why rebind the datagridview?
//this line should be removed
this.dataGridView1.DataSource = competitorDataSet.Growns;

//why call this here? validation should be done prior 
//to adding the new row to the datatable
//this line should be removed
this.Validate();

this.grownsBindingSource.EndEdit();
if (dataGridView1.BindingContext[competitorDataSet.Growns] != null)
{
dataGridView1.BindingContext[competitorDataSet.Growns].EndCurrentEdit();
}

//reverse the order of these 2 lines
this.grownsTableAdapter.Update(competitorDataSet.Odrasli);
this.grownsTableAdapter.Adapter.AcceptChangesDuringUpdate = true;
/* like this:
this.grownsTableAdapter.Adapter.AcceptChangesDuringUpdate = true;
this.grownsTableAdapter.Update(competitorDataSet.Odrasli);
 */

If this does not solve your problem, please post the binding code for your datagriview.

CristisS
  • 1,103
  • 1
  • 12
  • 31
  • Sadly, this doesn't work. I bind it with designer so I suppose, this is what you want to know (code in Form.Designer): `//grownsBindingSource: this.grownsBindingSource.DataMember = "Growns"; this.grownsBindingSource.DataSource = this.competitorDataSet;//competitorDataSet: this.competitorDataSet.CaseSensitive = true; this.competitorDataSet.DataSetName = "CompetitorDataSet";` and in datagridview line: `this.dataGridView1.DataSource = this.odrasliBindingSource;` – user2528094 Jul 24 '13 at 08:29
  • Here: and in datagridview line: this.dataGridView1.DataSource = this.odrasliBindingSource; shouldn't it be this.dataGridView1.DataSource = this.grownsBindingSource; ? – CristisS Jul 24 '13 at 09:04
  • My fault, didn't change it in english for better understanding. However, this is not the case. Still doesn't work. Is there any "trick" in properties for datagridview or datatable? – user2528094 Jul 24 '13 at 09:35
  • Did you try Chris's suggestion? – CristisS Jul 24 '13 at 09:36
  • Yes, I overlooked AcceptChanges after row adding. Thanks for help, now it works =) – user2528094 Jul 24 '13 at 09:45