views:

281

answers:

1

I have a WPF Window with several TextBoxes on it.

I have an XSD dataset attached to a SQL server database, and the window is bound to a row from the tableadapter:

  public partial class PersonForm : Window
  {
    public PersonForm(int id)
    {
      InitializeComponent();

      MyDatasetTableAdapters.personTableAdapter tableAdapter = 
        new MyDatasetTableAdapters.personTableAdapter();

      personRow row = tableAdapter.GetPersonById(id);

      this.DataContext = row;
    }

    ...

  }

The textboxes are bound like this:

<TextBox Name="lastName" Text="{Binding Path=lastname}" />

These binding bindings work and the text boxes are populated with the data from the SQL database properly, but when the text box is edited, the changes are not propogated back to the database.

I have a button that when clicked calls

myTableAdapter.Update(myRow);

This does successfully update the SQL database.

My question is: What changes do I need to make so that the text boxes will automatically update the database automatically (without having to explicitly call TableAdapter.Update).

Also, is this the proper way to bind a form to a database in WPF?

A: 

I've discovered one way to accomplish this.

By setting the NotifyOnSourceUpdated property to True on the textbox binding like this:

<TextBox Name="lastName" Text="{Binding Path=lastname, NotifyOnSourceUpdated=True}" />

I am then able to update the table adapter in the Window's SourceUpdated event like this:

private void Window_SourceUpdated(object sender, DataTransferEventArgs e)
{
  myTableAdapter.Update(myRow);
}

This seems to work well. Is there a better way to accomplish this?

Is there a way to enable the NotifyOnSourceUpdated on all text boxes easily without having to specify it on every TextBox?

Avalanchis