views:

1018

answers:

1

No database involved here, I am creating my own datatable in code, and binding it to a textbox and a datagridview.

When I change current record in the grid the textbox updates to the current value.

But what's the best way to synchronise changes between the values in the textbox and the datagrid. If I change one then it doesn't change the other unless I go to another record and back.

I can do this by adding a datagrid.Refresh() to the textbox Validated event, and presumably something to the CellValidated event on the datagrid, but it seems like I might be going about this the wrong way.

Edit:

Based on the answer below, my question should be: is there a way to notify bound controls of changes in a DataTable they are bound to, or must the code use a BindingList or do it manually instead.

+1  A: 

I suggest you use a BindingList<T> rather than a DataTable, where T is your "business object" that represents each record displayed in the grid. Then your business object should implement INotifyPropertyChanged and fire NotifyPropertyChanged whenever the value in the text box changes, either by binding the desired property to TextBox.Text or updating the appropriate property of the selected business object whenever TextBox.TextChanged fires.

flipdoubt
Thanks, that's an interesting approach that I will probably try (one day!). But would like to know the best way using a datatable as well - it really seems there should be an easy way to do this.
Simon D