简体   繁体   中英

Textbox Binding Update Event

I have a form with multiple textboxes on and a datatable, the datatable is bound to the textboxes and I am handling the RowChanged event of the datatable.

However the RowChanged event is only called twice when the form is loaded ( there are more than two textboxes ), the data from the datatable loads fine into the textboxes but when I change the text in the textboxes no event is triggered. Yet displaying the data directly from the datatable shows the data has been updated.

Code:

private Clients.DataSetClients.ClientsDataTable dtClients = new DataSetClients.ClientsDataTable();
private Clients.DataSetClientsTableAdapters.ClientsTableAdapter taClients = new DataSetClientsTableAdapters.ClientsTableAdapter();

    private void ClientsEdit_Load(object sender, EventArgs e)
    {
        dtClients.RowChanged += new DataRowChangeEventHandler(dtClients_RowChanged);

        taClients.FillByID(dtClients, ClientID);

        textForename.DataBindings.Add("Text", dtClients, "Forename", true, DataSourceUpdateMode.OnPropertyChanged);
        //.......
        // etc
        //.......
        textEmail.DataBindings.Add("Text", dtClients, "Email", true, DataSourceUpdateMode.OnPropertyChanged);
    }

    void dtClients_RowChanged(object sender, DataRowChangeEventArgs e)
    {
        MessageBox.Show("dtClients_RowChanged");
    }

    private void simpleButton1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(dtClients.Rows[0]["Email"].ToString());
    }

You can try wiring up the BindingComplete event of the binding instead of the RowChanged event on the DataTable (which is more for adding rows and row position changes, etc).

private void ClientsEdit_Load(object sender, EventArgs e)
{
  // loading stuff
  Binding ForenameBinding = new Binding("Text", dtClients, "Forename", true, DataSourceUpdateMode.OnPropertyChanged);
  ForenameBinding.BindingComplete += Table_BindingComplete;
  textForename.DataBindings.Add(ForenameBinding);
  // loading stuff
}

void Table_BindingComplete(object sender, BindingCompleteEventArgs e) {
  if (e.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate)
    MessageBox.Show("Source Updated!");
}

That MessageBox will pop up every time the user changes the text in the TextBox, so it would be firing on every key stroke. To avoid that, change your binding mode to OnValidation :

  Binding ForenameBinding = new Binding("Text", dtClients, "Forename", true, DataSourceUpdateMode.OnValidation);
  private void button1_Click(object sender, EventArgs e)
        {
            List<string> names=new List<string>(){"Forename","Email","Phone"};
            foreach (var name in names)
            {
                var txt = this.Controls["text" + name] as TextBox;
                txt.DataBindings.Add("Text", dtClients, name, true, DataSourceUpdateMode.OnPropertyChanged);
            }

        }

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