my grid items not updated but my itemssource is...

Mar 13, 2009 at 3:29 PM
Hi all,

here's my problem: i have a datagrid to which i'm binding my itemssource using a binding expression... (see code below)

If

 

chkActiveContact.IsChecked Then

 

 

 

b =

New Binding(".")

 

b.Mode = BindingMode.TwoWay

b.Source =

 

 

Me.Office.Contact_Office. _
                    Where(
Function(co As Contact_Office) _
                            (co.DateRemoved
Is Nothing OrElse co.DateRemoved > Date.Today) _
                            AndAlso co.Contact.isActiveContact)

grdContacts.SetBinding(DataGrid.ItemsSourceProperty, b)

 

 

 

 

Else

 

 

 

b =

New Binding(".")

b.Mode = BindingMode.TwoWay

b.Source =
Me.Office.Contact_Office

grdContacts.SetBinding(DataGrid.ItemsSourceProperty, b)

 

 

 

 

End If

When my binding source is b.Source = Me.Office.Contact_Office then i have No problem at all....

But when my binding source is the first one (with the where clause) that's where i have problems...

Now when i add an item in Me.Office.Contact_Office, my datagrid doesn't display the new added item.
Althought, if i look inside grdContacts.itemssource, i see the added item but it it not present in grdContacts.items.

So i thought to myself, well that must be normal because of my where clause so i should probably warn the grid to update itself using the itemssource.

So after my changes have been made, i do: 

 

grdContacts.GetBindingExpression(DataGrid.ItemsSourceProperty).UpdateTarget()


But this doesn't do anything... The only way i'm able to refresh correctly the data was to rebind again my itemssource but i don't think it's the efficient way of doing this...

I also tried to do grdContacts.items.refresh() without any success...


Any suggestions?

 

 

Coordinator
Mar 16, 2009 at 2:27 AM
I believe this looks like a case of not using ObservableCollection.  Try updating your source to use an ObservableCollection.  For example, source = new ObservableCollection<T>(expression.ToList()).
Mar 16, 2009 at 4:36 PM
Edited Mar 17, 2009 at 2:19 PM
It doesn't seem to work since my source is a dynamic query.

by doing your suggestion the source is not reevaluated when the query expression changes (since the source is a collection)

But believe that UpdateTarget should do what i want since my binding expression does know about the who's the target and what is my source (and the source in run-time got the right items)

grdContacts.GetBindingExpression(DataGrid.ItemsSourceProperty).UpdateTarget()

Coordinator
Mar 19, 2009 at 11:58 PM

The result of a LINQ query is just an IEnumerable, not INotifyCollectionChanged.  After you change the source of the query, you have to explicitly re-evaluate the query to see the new results and rebind to ItemsSource.

 

Mar 20, 2009 at 2:18 PM
Well that's what im doing right now to make it work...

But i thought that doing grdContacts.GetBindingExpression(DataGrid.ItemsSourceProperty).UpdateTarget() would actually re-evaluate my source (which is my dynamic query) and force data-transfert from my source to my target...

But i guess that in that case it only reeveluate only the objects that are currently in my source without carrying about the query...