Current filter:
                                You should refresh the page.

                                MVVM - How to bind the GridControl's selected rows to a property in a ViewModel

                                0

                                This example demonstrates how to bind GridControl's selected rows to a property in ViewModel in a MVVM-based application. The SelectionAttachedBehavior helper class used in this sample, provides a bindable SelectedItemsSource property, that can be used to define selection at the ViewModel level.

                                Note, we made the SelectionAttachedBehavior class as generic as possible, and the same approach can also be used for other controls that support multiple selection. This example demonstrates how this can be done when working with the standard ListBox control, as well as with the standard DataGrid.

                                • S Sharma 12.19.2012

                                  Hi

                                  I am using this procedure to preserve selected rows for the WPF GridControl, but as we are updating the grid with new data in a timer, the selected rows are still getting lost. Please suggest how should we modify this sample so as to preserve grid selected rows even between the updates (like same selected rows should be present after the grid is updated with new data in a separate thread). Also, We are using a NavigationStyle=Cell, so just wanted to also check if we need to change the NavigationStyle to Row for your below sample to work with grid updates?

                                  Thanks

                                You must  log in  or  register  to leave comments
                                Select file
                                • MainPage.xaml
                                • MainWindow.xaml
                                • MainPage.xaml.cs
                                • MainWindow.xaml.cs
                                • ViewModel.cs
                                Select language
                                • C#
                                • VB.NET
                                Select version
                                • v2012 vol 1.6 - v2013 vol 1.2
                                • v2010 vol 2.6 - v2010 vol 2.11
                                <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="SLGridMVVMSelection.MainPage"
                                        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
                                        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
                                        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
                                        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
                                
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <dxg:GridControl ItemsSource="{Binding Source}" AutoPopulateColumns="True">
                                            <dxg:GridControl.View>
                                                <dxg:TableView MultiSelectMode="Row" NavigationStyle="Row" SelectedRowsSource="{Binding Selection}" />
                                            </dxg:GridControl.View>
                                        </dxg:GridControl>
                                        <ListBox SelectionMode="Multiple" Grid.Column="1" ItemsSource="{Binding Source}"
                                                  dx:SelectionAttachedBehavior.SelectedItemsSource="{Binding Selection}" />
                                        <dxg:GridControl Grid.Column="2" ItemsSource="{Binding Source}" AutoPopulateColumns="True">
                                            <dxg:GridControl.View>
                                                <dxg:TableView MultiSelectMode="Row" NavigationStyle="Row" SelectedRowsSource="{Binding Selection}" />
                                            </dxg:GridControl.View>
                                        </dxg:GridControl>
                                        <Button Grid.Row="1" Grid.ColumnSpan="2" Content="Select Odd Rows" Command="{Binding SelectOddRowsCommand}"/>
                                        <Button Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Content="Delete Selected Rows" Command="{Binding DeleteSelectedRowsCommand}"/>
                                    </Grid>
                                </UserControl>