Manoj
Manoj

Reputation: 5087

Updating data to datagrid without data binding

Is it possible to update data in the datagrid without having to bind it to a particular object and still enjoy the benifits of a "two way" data binding? In this case the type of object to be binded is decided at runtime based on user input.

Some more clarity:

Actually I am using a multi threaded application and I need to update data in UI from another thread. For this purpose I share a reference for an Observable Collection object and bind(through ItemsSource) the same to the data grid. Now, whenever the thread updates the data, it calls a specific function in the UI thread asking it to refresh the datagrid. The problem arises when I try to modify some value in the grid so that it is sent back to the thread that is running in parallel. It throws an exception "'DeferRefresh' is not allowed during an AddNew or EditItem transaction."

Upvotes: 1

Views: 578

Answers (3)

andyhasit
andyhasit

Reputation: 15289

With a DataGridView you can change the datasource at runtime and still enjoy two-way binding... Just set the DataSource to null first.

If for some reason you can't do that, you might consider object composition: i.e. binding your grid to an intermediate object which simply holds a variable for the actual object you're 'binding' at runtime, and create wrappers for the implementation (IList, IListSource, IBindingList or IBindingListView).

Unless I've missed something?

Upvotes: 1

Vinit Sankhe
Vinit Sankhe

Reputation: 19885

When you set (not bind) the ItemsSource, bindings automatically happen at row and cell levels by internal implementation of DataGrid. So I am not able to understand "how" can we not bind an object to a datagrid and enjoy it's TWO WAY UPDATES.

They will anyways happen when u set the ItemsSource... or is it that you DONT want to even set the ItemsSource?

Upvotes: 0

Haris Hasan
Haris Hasan

Reputation: 30097

You can dynamically create a binding though code at runtime on bases of user input

Other than that I don't think that there would be any way to achieve two way binding without data binding. Once ugly way could be to handle data change event in both the itemssource and DataGrid and on the event update the other control i.e. data grid in case of change in itemssource and itemssource in case of changed value in datagrid, manually

Upvotes: 0

Related Questions