开发者

How to automatically update TableAdapter bound to TextBox

开发者 https://www.devze.com 2022-12-19 15:17 出处:网络
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:

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 bi开发者_StackOverflow社区ndings 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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号