Force a refresh

Apr 9, 2009 at 4:36 PM
Edited Apr 9, 2009 at 4:37 PM
 I have a master/detail datagrid which works very well at startup.

<
dg:DataGrid AutoGenerateColumns="False" x:Name="Customers" SelectionMode="Single" ItemsSource="{Binding}">
    <dg:DataGrid.RowDetailsTemplate>
        <DataTemplate >
            <dg:DataGrid x:Name="dgOrders" ItemsSource="{Binding Orders}" AutoGenerateColumns="False"  CanUserAddRows="False"/>
        </DataTemplate >
</
dg:DataGrid.RowDetailsTemplate>
</dg:DataGrid>

But I am not able to force the Orders details grid to refresh if a new order has been added to the database (not by a Row but by inserting a record into the database). The record is stored in the database but the Datagrid doesn't update.

Thanks a lot in advance for an idea how to do this.

 

 

 

 

Coordinator
Apr 10, 2009 at 10:00 PM
Hi Peter,

You need to update this in your implementation of your orders collection.  If you paste in that code we can take a look.

Thanks,
Alexis
Apr 11, 2009 at 6:28 AM

 

 

Thanks a lot for responding.

What I did was this:

<
dg:DataGrid AutoGenerateColumns="False" x:Name="Customers" SelectionMode="Single" ItemsSource="{Binding}">
    <dg:DataGrid.RowDetailsTemplate>
        <DataTemplate >
            <dg:DataGrid x:Name="dgOrders" ItemsSource="{Binding Orders}" AutoGenerateColumns="False"  CanUserAddRows="False"/>
                <dg:DataGrid.RowDetailsTemplate>
                    <DataTemplate
>
                        <dg:DataGrid.Columns
>
                            <dg:DataGridTextColumn Header="ID"  Binding="{Binding ID
}" />
                            <dg:DataGridTextColumn Header="CustomerID" Binding="{Binding CustomerID
}"/>
                        </dg:DataGrid.Columns
>
                        </dg:DataGrid>
        
                    </
DataTemplate
>
                </
dg:DataGrid.RowDetailsTemplate>
            </dg:DataGrid>
        </DataTemplate >
    </dg:DataGrid.RowDetailsTemplate>
    <dg:DataGrid.Columns>
        <
dg:DataGridTextColumn Header="CustID" MinWidth="40" Binding="{Binding CustID}" />
        <dg:DataGridTextColumn Header="CustCode" MinWidth="40" Binding="{Binding ICustCode}"/>
    </dg:DataGrid.Columns>
</dg:DataGrid>

What I was expecting is the Orders in the details DataGrid of just the customer in the masterDataGrid.

What I did for now is defining a CollectionViewSource that is the order-content in codebehind. This works fine.

 

 

private IEnumerable<Orders> queryOrders = null;
public
IEnumerable<Orders> CustomersOrders
{
    
get
    {
            queryOrders = from order in App.linqDataContext.GetTable<Customers>()
                            
where currentCustomer.CustID == order.CustomerID
                            select order;
            return queryOrders;
    }
}

 

 

I just thought that WPF/.NET 3.5 has some special power in Binding and this would result in a automatic filtering of my details table.

Apr 12, 2009 at 9:36 AM
dataGrid1.Items.Refresh();
Apr 12, 2009 at 2:26 PM
As far as I had tested "dataGrid1.Items.Refresh();" might work on the current data, but will not add a new order that has been added to the database.
Apr 12, 2009 at 5:46 PM
Why the "dataGrid1.Items.Refresh();" doesn't work in the following case?

Thanks,
Kamel

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DataGridWPF
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private Hashtable _hashtable;

        public Window1()
        {
            InitializeComponent();
            _hashtable = new Hashtable();
            dataGrid1.ItemsSource = _hashtable.Values;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Trade trade = new Trade();
            trade.Date = DateTime.Now;
            trade.Price = 20;

            _hashtable.Add(Guid.NewGuid(), trade);

            dataGrid1.Items.Refresh();
        }

    }

    public class Trade
    {
        public double Price { get; set; }
        public DateTime Date { get; set; }
    }
}
Coordinator
Apr 14, 2009 at 9:50 PM
DataGrid.Items.Refresh() doesn't actually call back to the database - it just recreates the collection view.  In order to update the items, you need to manually requery your database and reset the ItemsSource with the new data.