c# Database not updating from databindings
I'm relatively new to Windows Forms and having trouble getting a form with multiple text fields to update the database.
I fill the dataset, add the data binding to each field and add a leave event to each field that updates the database.
clientsTableAdapter.FillByID(dataSetClients.Clients, tempID);
txtForename.DataBindings.Add("Text", dataSetClients.Clients, "Forename");
txtForename.Leave += new EventHandler(updateDataSet);
private void updateDataSet(object sender, EventArgs e)
{
this.clientsTableAdapter.Update(this.dataSetClients.Clients);
}
The database does not update, I have tried this in many different ways and the only way that seems to work is if I update the dataset manually then ru开发者_运维问答n .update() on the adapter, like so;
this.dataSetClients.Clients.Rows[0]["Forename"] = "New Forename";
this.clientsTableAdapter.Update(this.dataSetClients.Clients);
Any help or guidance on the subject is greatly appreciated.
The default DataSourceUpdateMode for DataBindings is OnValidation: as the Validation events aren't called until after the Leave event, the values aren't updated.
Specifying DataSourceUpdateMode.OnPropertyChanged
for the DataBindings should work.
not sure if this problem is still on, I just want to contribute the things which helped me. I had the same problems with DataSet, Databinding and Data which was changed in the DataSet but not in the Database. I tryed out all this tips but nothing seemed to help.
DataSet.Data.Tables[0].Rows[0].EndEdit(); //0 for your Index/Tablename
That helped me, so I could bind the Data to the Dataset and then, when I wanted to Update the Data, I set "EndEdit()" to every table in the Current DataSet.
I also tryed this:
foreach (Control bla in DisplayDict[SubTyp.ToString()].DataControlHelper.Values)
{
bla.BindingContext[DisplayDict[SubTyp.ToString()].DBData.Data].EndCurrentEdit();
}
which made no Change, and I also tryed .GetChanges() and .HasChanges() but the .EndEdit() was the one Thing missing in my Code.
I hope this helps someone.
P.S. Sorry for my bad english, it has been a while since I last used it (:
精彩评论