0

I want to refresh my data grid when button click (MVVM Model). In my view model there is no reference about view. Can anybody explain.

i want to use DataGrid.Refresh() method when button click. How can i use this in MVVM Model.

Joby James
  • 429
  • 8
  • 22
  • http://stackoverflow.com/questions/5285377/wpf-mvvm-inotifypropertychanged-implementation-model-or-viewmodel – Dani Jul 03 '15 at 11:40
  • first in mvvm there is no there is no button click, second i want to refresh my DataGrid. – Joby James Jul 03 '15 at 11:49

4 Answers4

1

Set your data item to ObservableCollection and bind itemSource of dataGrid with ObservableCollection. Datagrid will be refreshed if entries will be added/removed or moved to this collection.

Gul Ershad
  • 1,743
  • 2
  • 25
  • 32
1

you need to databind your DataGrid to some collection in viewmodel:

<DataGrid ItemsSource="{Binding Items}">...</DataGrid>

then, if your Items property is of type ObservableCollection, DataGrid is refreshed automatically, when items are added or removed from Items collection. There is no need to call DataGrid.Refresh() - this is why MVVM makes things simpler.

public class MainWindowViewModel : ViewModelBase
{
    public MainWindowViewModel()
    {
        Items = new ObservableCollection<SomeClass>();
        //add some test data
        Items.Add(new SomeClass());
        Items.Add(new SomeClass());

        RefreshCommand = new DelegateCommand(Refresh);
    }

    public DelegateCommand RefreshCommand { get private set; }

    public ObservableCollection<SomeClass> Items { get; private set; }

    private void Refresh()
    {
        Items.Clear();

        //add actual items
        Items.Add(new SomeClass());
        Items.Add(new SomeClass());
    }
}

Alternatively, you could cust create new instance of Items collection:

    private void Refresh()
    {
        //in this case, items doesn't have to be ObservableCollection, but any collection type
        Items = new ObservableCollection<SomeClass>             {
           new SomeClass(),
           new SomeClass()
        };
        OnPropertyChanged("Items");
    }

if you really need to access UIElement, then do it from codebehind, when something happens in viewmodel (use viewmodel event to notify view, that something has happened). In following sample I have used PropertyChanged event to notify view, that something in viewmodel has changed and view takes care of refreshing viewmodel.

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow 
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new MainWindowViewModel();
        ((MainWindowViewModel) DataContext).PropertyChanged += ViewModel_PropertyChanged;
    }

    void ViewModel_PropertyChanged(object s, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "Items")
        {
            MyDataGrid.Refresh();
        }
    }
Liero
  • 25,216
  • 29
  • 151
  • 297
0

I am using DataTable bound to DataGrid. DataGrid wouldn't update UI if I added column to DataTable. To force it to do through ViewModel, I set the DataTable Property to null first(save it temporarily) and then set it back to the original DataTable. This worked for me.

joe_maya
  • 185
  • 1
  • 7
0

I know its a little late for this, but this is my workaround to refresh DataGrid in wpf mvvm:

private void RefreshDataGrid()
        {
            InvoiceModel temp;
            temp = Invoice;
            Invoice = null;
            Invoice = temp;
        }

This is XAML:

<DataGrid ItemsSource="{Binding Invoice.Items}" SelectedItem="{Binding SelectedItem}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False">

As you see its just a reassigning Invoice which provides ItemSource for DataGrid and it easily refreshes the DataGrid.

Safa Seed
  • 35
  • 9