How can I select all expanded/visible rows in a radgridview control, which have been grouped and possibly filtered, even if the rows fall out of view of the window container?

1 Answer 59 Views
Data Virtualization GridView
Shannon
Top achievements
Rank 1
Shannon asked on 23 Feb 2023, 04:43 PM

Hello,

How can I select all expanded/visible rows in a radgridview control, which have been grouped and possibly filtered, even if the rows fall out of view of the window container?

I have a window that contains a radgridview and a button that is outside of the grid which selects or deselects all visible rows in the radgridview. An end user may add multiple groupings and filters. There is a lot of data in the item source, at the time of this test over 3 thousand rows. The resulting data display after the end user has made changes will likely exceed the window containers viewable area. when I implemented the line of code below only the expanded row visible in the window was selected. The expanded rows that I had to scroll down to see were not selected. I would like to select all of the expanded/visible rows.

 var visibleRows = grdBatches.ChildrenOfType<GridViewRow>();

I have read that ChildrenOfType<T> works on visual elements. In another forum question, the answer stated that the use of visual elements is not advised when working with virtualized controls such as the radgridview.


Shannon
Top achievements
Rank 1
commented on 23 Feb 2023, 07:33 PM

I found the answer in the Telerik article linked below. However, Telerik stongly advises against this solution. Can anyone help me come up with another solution? 

I set the Radgridview EnableRowVirtualization property to false and now I am able to select all expanded/visible rows using the code below.

var visibleRows = grdBatches.ChildrenOfType<GridViewRow>();

However,  the UI virtualization article in the link below strongly advises against disabling UI virtualization due to performance issues and increased loading time. It is highly not recommended.

https://docs.telerik.com/devtools/wpf/controls/radgridview/features/ui-virtualization?_ga=2.152316729.1623548671.1676991592-610389054.1563760323&_gl=1*7tv5a5*_ga*NjEwMzg5MDU0LjE1NjM3NjAzMjM.*_ga_9JSNBCSF54*MTY3NzE2NTE1MC4zNC4xLjE2NzcxNjc1NTkuNTguMC4w

 

Thank you,

 

 

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 24 Feb 2023, 12:23 PM

Hello Shannon,

To select rows, I recommend you to use the selection API of RadGridView. For example, you can use the SelectedItems collection property of RadGridView or the Select method. You can use this API to select data items from the ItemsSource collection, instead of using the GridViewRow visual containers.

To get all items that are present in the current data view (after filtering), use the Items collection property of RadGridView.

So, to select all available rows after filtering, you can add all items from the Items collection to the SelectedItems collection.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Shannon
Top achievements
Rank 1
commented on 27 Feb 2023, 03:16 PM

Thank you. I will try this and then reply with an update.
Shannon
Top achievements
Rank 1
commented on 28 Feb 2023, 04:46 PM

Hello,

I'm going to restate my issue because the original description is not completely accurate. Let's disregard the row selection.  

My actual issue is finding the visible/expanded rows even if they have fallen outside of the window container or as you stated, getting all items that are present in the current data view (after filtering). 


The code below only works if I set the grid property EnableRowVirtualization to false. However, Telerik strongly advises not disabling virtualization on the radgridview visual element because it may cause performance and loading issues.
 var visibleRows = grdBatches.ChildrenOfType<GridViewRow>();

What property or method would return all visible/expanded rows?

Thank you,  

Martin Ivanov
Telerik team
commented on 01 Mar 2023, 11:40 AM

You can't get all GridViewRow controls when the UI virtualization is on. This is because in that case only the rows in the viewport are generated. When you scroll up or down, the same GridViewRow instances will be re-used. Only their data item is replaced and they are re-arranged according to the new viewport. In case the viewport size changes, new row visuals will be generated or removed.

In order to work with the rows in gridview, you should use data bindings for their properties or to the elements in their data templates (like the CellTemplate of the column). This way you can work with the data items of the ItemsSource or the Items collection.

To select all rows in the current data view, whether their GridViewRow visuals are rendered, you can call the SelectAll method of RadGridView. Or alternatively, you can manually add all items from the RadGridView's Items collection to the SelectedItems collection. But note that this will work only if the SelectionMode of RadGridView is set to Multiple or Extended.

If this information doesn't help, can you send over few pictures and some code snippets showing your setup and the desired result?

Shannon
Top achievements
Rank 1
commented on 01 Mar 2023, 09:31 PM

Thank you, Martin. Here is the code that I am working with. I didn't save any code from testing.

<telerik:RadGridView
                x:Name="someGrid"
                Grid.Row="1"
                Grid.ColumnSpan="3"
                Height="450"
                Margin="0"
                telerik:StyleManager.Theme="Expression_Dark"
                AutoGenerateColumns="False"
                CanUserInsertRows="False"
                IsReadOnly="True"
                ItemsSource="{Binding someObservableCollection, Mode=TwoWay}"
                ScrollMode="RealTime"
                SelectionMode="Multiple"
                SelectedItem="{Binding SelectdItem, Mode=TwoWay}">
                <telerik:RadGridView.Columns>
                   
                    <telerik:GridViewDataColumn Width="60">                        
                        <telerik:GridViewDataColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox HorizontalAlignment="Center" IsChecked="{Binding IsActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                            </DataTemplate>
                        </telerik:GridViewDataColumn.CellTemplate>
                    </telerik:GridViewDataColumn>
                    <telerik:GridViewDataColumn
                        Width="60"
                        DataMemberBinding="{Binding Id}"
                        Header="Number" />
                    <telerik:GridViewDataColumn
                        Width="200"
                        DataMemberBinding="{Binding Name}"
                        Header="Name" />
                    <telerik:GridViewDataColumn
                        Width="200"
                        DataMemberBinding="{Binding Action}"
                        Header="Action" />
                    <telerik:GridViewDataColumn
                        Width="100"
                        DataMemberBinding="{Binding EffectiveDate, StringFormat='{}MM-dd-yyyy'}"
                        Header="Action Dates" />
                    <telerik:GridViewDataColumn
                        Width="200"
                        DataMemberBinding="{Binding Program}"
                        Header="Program" />
                    <telerik:GridViewDataColumn
                        Width="100"
                        DataMemberBinding="{Binding ValidateDate, StringFormat='{}MM-dd-yyyy'}"
                        Header="Validate Dates" />
                    <telerik:GridViewColumn Header="Delete" >
                        <telerik:GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <telerik:RadButton
                                    Content="Delete"
                                    Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:GridViewDataControl}}}"
                                    CommandParameter="{Binding}" />
                            </DataTemplate>
                        </telerik:GridViewColumn.CellTemplate>
                    </telerik:GridViewColumn>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
            <Grid
                Grid.Row="2"
                Grid.ColumnSpan="3"
                Margin="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <telerik:RadDataPager
                    x:Name="radDataPager"
                    AutoEllipsisMode="Both"
                    DisplayMode="All"
                    FontSize="12"
                    NumericButtonCount="10"
                    PageSize="50"
                    Source="{Binding Items, ElementName=someGrid}" />
        private void BtnSelectAll_Click(object sender, RoutedEventArgs e)
        {
            bool isActive = btnSelectAll.Content.ToString() == "Select All" ? true : false;
            List<int> visbileRowsIds = new List<int>();
            var visibleRows = someGrid.ChildrenOfType<GridViewRow>();
            ObservableCollection<Type> gridTypeCollection = (ObservableCollection<Type>)someGrid.ItemsSource;
           
            if (visibleRows.Count() > 0)
            {
                visbileRowsIds = visibleRows.Select(p => (Type)p.DataContext).Select(p => p.Id).ToList();
                foreach (var item in gridTypeCollection)
                {
                    foreach (var x in visbileRowsIds)
                    {
                      item.IsActive = item.Id == x ? isActive : item.IsActive;
                    }
                }
                btnSelectAll.Content = btnSelectAll.Content.ToString() == "Select All" ? "UnSelect All" : "Select All";
            }
        }
Martin Ivanov
Telerik team
commented on 02 Mar 2023, 02:01 PM

I can see that you have paging support in the view. Do you need to select the items on all pages, or only the ones on the current page?
Shannon
Top achievements
Rank 1
commented on 02 Mar 2023, 02:19 PM

I only need to select the items on the current page.
Martin Ivanov
Telerik team
commented on 02 Mar 2023, 03:08 PM

So, you don't mean to select the items as in the context of RadGridView selection feature, but instead to raise the IsActive flag of the data items. Please correct me if I am wrong. 

I've attached a sample project based on your code, that shows how to get the desired effect. I hope that helps.

Shannon
Top achievements
Rank 1
commented on 02 Mar 2023, 04:19 PM

Correct, a user will apply filters and grouping to the radgridview.

However, for every row open on the page, even if it is outside of the viewable container, I need to check the is active checkbox.

 

Martin Ivanov
Telerik team
commented on 06 Mar 2023, 12:38 PM

Thank you for the picture. In that case, you will need to extend the code from my previous example. Basically, if grouping is applied, you can iterate through all groups and mark only the expanded items as active. I've updated my project to show this. I hope it helps.
Shannon
Top achievements
Rank 1
commented on 06 Mar 2023, 08:11 PM

You are so awesome. Thank you very much. The code you provided worked. It does everything that I needed!
Shannon
Top achievements
Rank 1
commented on 06 Mar 2023, 08:20 PM

What happens next? Is there a place to mark this question as answered or will I get a survey?
Martin Ivanov
Telerik team
commented on 07 Mar 2023, 07:13 AM

Great to hear that the project was useful.

You can mark only answers as ...well, Answer. However, the communication until now is in the comment section of your original Question, so you cannot mark it as an Answer. I can convert my comment with the solution to an Answer, but this will remove it from the comments and the communication at the end may look a bit odd. Currently, lets leave it that way.

About getting a survey, keep in mind that this is the public forum where the main idea is for the community to be active, to help each other and to discuss ideas. The Telerik stuff is helping as much as possible, but we don't promise any answers in the forums. If you need a dedicated support, you can use the ticketing system in your telerik.com account. This way you can rate the communication there and leave feedback.  

Shannon
Top achievements
Rank 1
commented on 08 Mar 2023, 06:38 PM

Ok, thank you!
Tags
Data Virtualization GridView
Asked by
Shannon
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or