开发者

Silverlight 4 datagrid not re-sorting

I'm new to both Silverlight and RIA. I have a simple form with a DataGrid bound to a DomainDataSource object. The rows displayed represent section headings to be displayed on a webpage. One of the columns is called OrdinalPosition and I have specified that the grid is to sort by this column. I have a custom column with up and down arrow buttons. The desired behavior is that when the user clicks the up/down buttons the OrdinalPosition is incremented/decremente开发者_StackOverflowd so that they can specify what order the sections appear in.

If I manually change the value in the OrdinalPosition column, as soon as I move off the row the grid reorders itself. However, if I use code-behind to change the value the grid does not reorder itself (even though the grid does display the new value.) Here is my codebehind for the button click...

    private void incrementOrdinal(object sender, System.Windows.RoutedEventArgs e)
    {
        Button btn = (Button)sender;
        Section s = (Section)sectionDataGrid.SelectedItem;
        s.Ordinal++;
        sectionDataGrid.CommitEdit();
    }

Is there something I should be doing to cue the grid to reorder its records?


DomainDataSource will not re-sort data automatically when the records change unless the IEditableCollectionView interface is used to apply the changes through the data that the DomainDataSource exposes through its Data or DataView.

Try something like the following:

IEditableCollectionView view = (IEditableCollectionView)selectionDataGrid.ItemsSource;
Section s = view.CurrentItem;
view.EditItem(s);
s.Ordinal++;
view.CommitEdit();

This is what DataGrid performs when doing edits through the UI. The ItemsSource is bound to the DomainDataSource.Data property, which is an instance of the DomainDataSourceView class, representing the IEnumerable data that was loaded. The DomainDataSourceView implements IEditableCollectionView and when CommitEdit is called against that view after using EditItem, it will re-sort the data on the current page.

Note that when there are changes, re-sorting locally will not allow items to move onto or off of the current page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜