DataGrid UI doesn't refresh after sorting rows - need help

May 8, 2009 at 8:12 AM
Edited May 13, 2009 at 2:39 AM

Hi, I have dataGrid in my UI and also two buttons <MoveUp> and <MoveDown>.

Button - MoveUp - click - action - moves the selected row to the upper level

private void OnButtonMoveUpClick(object sender, RoutedEventArgs args)
            if (myDataGrid.SelectedIndex > 0)
                myEntriesLocalCopy.Move(myDataGrid.SelectedIndex, myDataGrid.SelectedIndex - 1);

Button - MoveDown - click - action - moves the selected row to the one row down.

private void OnButtonMoveDownClick(object sender, RoutedEventArgs args)
            if ((myDataGrid.SelectedIndex >= 0) && (myDataGrid.SelectedIndex < myDataGrid.Items.Count))
                myEntriesLocalCopy.Move(myDataGrid.SelectedIndex, myDataGrid.SelectedIndex + 1);

Problem: When the page is first loaded and If I click on moveUp or Down button, it works fine. But when I sort the grid by clicking on any column header and then try to click on any button (moveUp or Down), it doesn't work. I debug the code and I can see that actual items in collection which I have binded is changed, I mean moving does happen in collection (ObservableCollection) but UI doesn't refresh.

So, I am guessing something happens after sorting of rows in grid internally but I can't exactly what is going wrong.

If anybody can help me in this than it would be much appreciated.

Thanks in advance......

May 13, 2009 at 2:40 AM

Is there anybody that can help me in this?

May 13, 2009 at 12:42 PM

I have also experienced problems with the UI not being updated correctly. Try these three different methods and see if any of them help:

  1. myDataGrid.Items.Refresh();
  2. CollectionViewSource.GetDefaultView(myDataGrid.ItemsSource).Refresh();
  3. myDataGrid.ItemsSource = null;
    myDataGrid.ItemsSource = yourCollection;
May 14, 2009 at 2:24 AM
Edited May 14, 2009 at 2:25 AM

Thanks oskarh.

From your listed options the last one work fine for me but only problem using that is getting buch (same as number of columns) of errors displays on output window like

System.Windows.Data Error: 39 : BindingExpression path error: 'Description' property not found on 'object' ''Object' (HashCode=8761822)'. BindingExpression:Path=Description; DataItem='Object' (HashCode=8761822); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

I know that this error occurs because of it trys to bind default empty row in my grid. Is there any way to get rid of this error because I want that empty row to allow the user to enter new row. I looked for solution in this forum but i couldn't find any.

Thanks again..

May 14, 2009 at 7:27 AM

The empty row should be instantiated when editing is initiated on it. Have you tried simply add a new entry using it?

All those errors are annoying but in this case you should be able to safely ignore them. It would be even better if the DataGrid didn't try to data bind the cells in the empty row until it is instantiated, though.

May 15, 2009 at 2:24 AM

Yes I agree with you mikj. Datagrid shouldn't bind new default row until it is instantiated. I think we need to wait until next WPF DataGrid release (may be end of this year).