简体   繁体   中英

Textbox values not changing on BindingContext Changed event

Currently to change the record displayed in textfields in the form on the click of next button, I am programmatically assigning values of next record to the relevant textboxes using below code:

int CurrentRowIndex = this.BindingContext[DS1, "Table1"].Position;
int NextRoecordIndex = CurrentRowIndex + 1;

I am then using the NextRecordIndex ot populate values in the textboxes from the DataSet. I want that as soon as the below code is executed on click the next button, next record should automatically get populated.

this.BindingContext[DS1, "Table"].Position -= 1;

Are you listening to the BindingContextChanged event? Maybe you need to add an event delegate to the textbox's BindingContextChanged event or to override in it's derived class..

You need to implement the event handler. To do so create the member function which shall be called each time the event occurs in your text control. To listen to this event attach (register) your created member function to the event delegate of your textbox. Now every time the textbox fires its BindingContextchanged event all functions attached to its delegate are invoked.

Create the function to be called each time the event occours.

private void AssignNextRecordIndex_OnBindingContextChanged(object sender, EventArgs e) 
{
    // do ur task on bindingcontext changed
}

Add or register to listen to the event:

MyTextBox.BindingContextChanged += AssignNextRecordIndex_OnBindingContextChanged;

Thats it.

To detach or to stop listening to this event (if neccessary) use:

MyTextBox.BindingContextChanged -= AssignNextRecordIndex_OnBindingContextChanged;

You can attach as many functions to events as you like.

You also find information on this special event on MSDN: Control.BindingContextChanged Event

And also some basics about events in .NET Events (C# Programming Guide)

In case you created your own custom TextBox control like:

class MyCustomTextBox : TextBox
{...}

you can override the inherited OnBindingContextChanged method instead (don't forget to call the base method):

protected override void OnBindingContextChanged(EventArgs e)
{
    base.OnBindingContextChanged(e);
    // do your stuff
}

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