简体   繁体   中英

Diagnosing WinForms Databinding problem

I'm learning Databinding from here http://www.akadia.com/services/dotnet_databinding.html

I'm just updating the dataset and then updating the sql database using sqlDataAdaper.Update();

    this.selectCommand = new SqlCommand();
    this.selectCommand.CommandType = CommandType.Text;
    this.selectCommand.Connection = this.connection;

    this.dataAdapter = new SqlDataAdapter(this.selectCommand);
    this.dataAdapter.MissingSchemaAction = System.Data.MissingSchemaAction.AddWithKey;

    this.commandBuilder = new SqlCommandBuilder(this.dataAdapter);
    this.selectCommand.CommandText = "Select `FirstName`,`lastname` from customers where customerId=123";
    this.dataAdapter.Fill(this.dataSet, "customers");

now binding as:

// Simple Data Binding
txtBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet, "Customers.FirstName", true));

Its showing the firstname and when I change the first name and hit save button following code runs:

    this.dataAdapter.Update(this.dataSet, "customers");

changed value is not reflected in the dataset and thus not in the database too. I don't understand why?

When will the BindingContext push the value that I changed in Textbox (GUI) into the dataset? Am I missing anything?

On clicking save button do this:

   CurrencyManager  cm = (CurrencyManager)this.BindingContext[dataSet, "customers"];
   cm.EndCurrentEdit();
   this.dataAdapter.Update(this.dataSet, "customers");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM