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.
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:
I suspect your problem is in the following code block (explanations in code comments):
If this does not solve your problem, please post the binding code for your datagriview.
When you set
this.Update(competitorDataSet.Odrasli);
theTableAdapter
updates the changes fromDataTable
(news, deleted, updated rows) to the database.Since you call
competitorDataSet.Growns.AcceptChanges();
beforeTableAdapter.Update
, all changes in the table are already accepted and TableAdapter has nothing to update.So just remove
Also, if you set
this.grownsTableAdapter.Adapter.AcceptChangesDuringUpdate = true
beforegrownsTableAdapter.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)