This is a migrated thread and some comments may be shown as answers.

Select a row in a radGridView & RadDataPager

27 Answers 795 Views
DataPager
This is a migrated thread and some comments may be shown as answers.
Julien
Top achievements
Rank 1
Julien asked on 28 Feb 2011, 01:55 PM
Hi!

I've actually a radGridView and a RadDataPager.

The source of the RadDataPager.Source is bound to the radGridView.items.

It was working fine, but I found a problem: Sometimes I have to switch between tabs and select a specified object.

I'm using the RadGridView.SelectedItem =TheNewItem.

But if the new item is on another page, it just doesn't select anything. So how can I do that?

thank you!

27 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 28 Feb 2011, 02:15 PM
Hi Julien,

When the grid is being paged by the pager, it is aware of the current page only. You can imagine this as the pager changing the ItemsSource of the grid each time the user moves to a different page. In other words, the grid is not even aware that someone is paging it, i.e. it does not see the "big picture" with all the pages of data -- it only knows that it has 10 items in its ItemsSource (if the PageSize is 10 for example).

So if you want to select an item that is on a totally different page, you will have to calculate on which page is this item, move the pager to this page by using its public API and then select the item.

I hope this helps.

Regards,
Ross
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Julien
Top achievements
Rank 1
answered on 01 Mar 2011, 07:41 AM
Hum okay,

But in facts, it's my radDataPager which is bound on the radgridview. So I thought than if it is able to change his datasource, it might be able to see what we set for the selected item and load the corresponding page.
0
Julien
Top achievements
Rank 1
answered on 01 Mar 2011, 08:15 AM
And how can I know the position of my items because its position is depending of the sort/group/... of the radgridview
0
Rossen Hristov
Telerik team
answered on 01 Mar 2011, 09:34 AM
Hello Julien,

RadDataPager works solely through the IPagedCollectionView interface. The actual data is not "flowing" through the pager. Since RadGridView.Items implements IPagedCollectionView, the pager is simply issuing command like "go to the next page", "return to the first page". But the pager itself does not know anything about data.

Please read my blog post about paging.

I understand that it is not very trivial to find the page, but this is the only way -- the pager is unaware of data and it is not its job to deal with selection. So it would be up to you to decide which page you want to go to and then simply instruct the pager to go there.

I hope this makes sense.

Kind regards,
Ross
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Julien
Top achievements
Rank 1
answered on 01 Mar 2011, 09:50 AM
So, tell me how I can know the position of an object in a RadGridView? Because I don't see how with exposed collections/methods
0
Rossen Hristov
Telerik team
answered on 01 Mar 2011, 10:03 AM
Hello Julien,

Let me try to explain this again. Imagine that you have the following data: 4 2 5 1 and a page size of 2.

So you will have [4 2] on the first page and [5 1] on the second page. Now imagine that you add sorting and the pages become [1 2] and [4 5].

Now you are on the first page, i.e. you see [1 2] and you insert a new item 3 to the source collection. The pages will now become [1 2] [3 4] [5]. You are still on the first page and you are still seeing [1 2].

You will need to calculate that the new item 3 will be in fact on the second page and call RadDataPager.MoveToPage(1) and then RadGridView.SelectedItem = 3;

RadGridView and RadDataPager do not possess the functionality to calculate this for you. They "know" only about the current page, i.e. [1 2]. They don't know what will be coming next when you move the pager to another pager. They are simply unaware of anything "living" outside the current view.

In case you can't calculate the math I described above -- I am afraid that what you want to achieve would be impossible, i.e. you will not be able to do it.

I hope this helps.

All the best,
Ross
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Paul
Top achievements
Rank 2
Iron
Iron
Iron
commented on 07 Dec 2021, 03:46 PM

What does one use instead of RadDataPager.MoveToPage in the ViewModel when using the MVVM pattern?
Dilyan Traykov
Telerik team
commented on 07 Dec 2021, 04:38 PM

I believe you can bind the PageIndex property of the control to a property in your viewmodel to achieve the desired result:

<telerik:RadDataPager PageIndex="{Binding PageIndex, Mode=TwoWay}" ...

You can then change the value of this property from your viewmodel like so:

        private void MoveToPage(int pageIndex)
        {
            this.PageIndex = pageIndex;
        }

Paul
Top achievements
Rank 2
Iron
Iron
Iron
commented on 08 Dec 2021, 09:25 AM

Great, that's exactly what I was looking for, thanks Dilyan
0
Julien
Top achievements
Rank 1
answered on 01 Mar 2011, 10:20 AM
Hum.... I've fully understood what you mean. I know that I've to compute the the current page. I can do this easily with the number of item per page, but I need to know the position of an element in a list.

The only thing I'm missing to compute the current page is a collection in which I've elements in the current. And I think that the RadGridView should be aware of that. 

Since my bounded collection don't get its order changed when the radGridView sort/filter/group... I cant use it to get the IndexOf my current object.

0
Rossen Hristov
Telerik team
answered on 01 Mar 2011, 10:52 AM
Hi Julien,

Come to think of it, if there is not sorting the calculation could be done on the original source collection, i.e. the one with the "raw" data.

But with sorting I don't think that the calculation could be done. When there is sorting and paging, the data engine will create a LINQ query like this:

var view = <<sourceCollection>>.OrderBy(item=>item.ID).Skip(20).Take(10). // PageSize 10, PageIndex 2

So in fact RadGridView.Items will show this view.

I am afraid that when you have sorting, filtering or grouping involved you will not be able to achieve your task.

Please, excuse me for misleading you in the first place.

Best wishes,
Ross
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Julien
Top achievements
Rank 1
answered on 01 Mar 2011, 11:01 AM
But there is now way to get  the list as the data engine see it, before the skip/take? It will earn me a lot of work

Otherwise I will not have only to manage sort, but sort, filter, groups, on all my fields, of all my views. ... all things which are added by telerik on the gridView. It starts to make a lot of work only to select an item...

You understand that it means that I've to interprete your SortDescription/FilterDescription/GroupDescription myself then, while you have already theses informations in your components
0
Rossen Hristov
Telerik team
answered on 01 Mar 2011, 12:50 PM
Hello Julien,

The only way that could be possible would be to transform the original collection to an IQueryable using the AsQueryable extension method. Once you have the original collection as an IQueryable you can call our data engine's extension methods which are public. Here is what our QueryableCollectionView does internally in order to create the view:

queryable = queryable.Where(this.FilterDescriptors);
queryable = queryable.Sort(queryable);
queryable = queryable.Select(this.SelectDescriptors);
queryable = queryable.GroupBy(this.GroupDescriptors);
queryable = queryable.Page(this.PageIndex, this.PageSize);
 
return queryable;

You will do something similar but without the paging.

So you can call all of the above methods (or the ones that you need) on the IQueryable except for the last one -- the one about paging. You will take the descriptors from the grid, i.e. RadGridView.SortDescriptors, etc. This should return the data filtered, sorted, projected and grouped. Have in mind that if you have grouping the paging will be done over the groups and not over the individual items.

These extension methods are found in the QueryableExtensions class and are public.

I hope this helps.

All the best,
Ross
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Julien
Top achievements
Rank 1
answered on 01 Mar 2011, 01:29 PM
Hi,

This sounds really better, all my collections are already LINQ collections, but:

I don't have a "SelectDescriptors" on a RadGridView

Other things seems to work(I didn't test it yet), but the compilator seems to be satisfied with.

Julien
0
Rossen Hristov
Telerik team
answered on 01 Mar 2011, 01:40 PM
Hi Julien,

You don't need the SelectDescriptors. Currently they are not used by the grid.

As for the FilterDescriptors you need to have the using directive:

using Telerik.Windows.Data;

I hope this helps.

Regards,
Ross
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Julien
Top achievements
Rank 1
answered on 01 Mar 2011, 02:33 PM
Hi

I'm sorry, I edited my previous post, but not fast enough :P

I've made a method which is able to select an item in a radGridView with a RadDataPager, it works only for one level of groups
/// <summary>
        /// Selects the item.
        /// </summary>
        /// <param name="grid">The grid.</param>
        /// <param name="pager">The pager.</param>
        /// <param name="objectToSelect">The object to select.</param>
        /// <param name="objectCollection">The object collection.</param>
        public static void SelectItem(RadGridView grid, RadDataPager pager, object objectToSelect, IQueryable objectCollection)
        {
            objectCollection = objectCollection.Where(grid.FilterDescriptors);
            objectCollection = objectCollection.Sort(grid.SortDescriptors);
            objectCollection = objectCollection.GroupBy(grid.GroupDescriptors);
 
            int pageIndex = 0;
            if (!grid.IsGrouping)
            {
                pageIndex = (int)Math.Floor((double)objectCollection.ToIList().IndexOf(objectToSelect) / pager.PageSize);
            }
            else
            {
                int groupIndex = Enumerable.Cast<Group>(objectCollection).TakeWhile(group => !group.Items.Cast<object>().Any(item => item == objectToSelect)).Count();
                pageIndex = (int)Math.Floor((double)groupIndex / pager.PageSize);
            }
            if (pageIndex >= 0)
                pager.PageIndex = pageIndex;
            grid.SelectedItem = objectToSelect;
        }


The only missing thing is how to "open" the group in which is my object, how can I open it if I've the reference?

Thank you
0
Rossen Hristov
Telerik team
answered on 01 Mar 2011, 02:45 PM
Hello Julien,

The Group has an Items property that will return its children -- either data objects or other groups.

By the way, you can use your Visual Studio IntelliSense to find other useful public API's.

I hope this helps.

Regards,
Ross
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Julien
Top achievements
Rank 1
answered on 01 Mar 2011, 02:52 PM
I know that, my question was how to "Open" it, I mean to expand it in the GUI, when I set the currentitem/SelectedItem, we cannot see it because the group is not expanded
0
Rossen Hristov
Telerik team
answered on 01 Mar 2011, 03:01 PM
Hi Julien,

Sorry for the misunderstanding. You can try the following:

1. Find the GridViewGroupRow that correspond to the respective Group:

var groupRow = (GridViewGroupRow)this.radGridView.ItemContainerGenerator.ContainerFromItem(group));

2. Set its IsExpanded property to true:

groupRow.IsExpanded = true;

Alternatively you can configure the whole grid with its AutoExpandGroups property.

Greetings,
Ross
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Julien
Top achievements
Rank 1
answered on 02 Mar 2011, 12:29 PM
Hi,

I can't use the AutoExpand because it will expand all my items, and this isn't wanted.

I've a problem, through the iqueriable, I can have a group which contains all my elements, but in facts its not the same instance than the group of your component, then ContainerFromItem return null.

How can I access to your internal groups lists? I know the index of the group and one item contained into it.

Thank you
0
Accepted
Rossen Hristov
Telerik team
answered on 02 Mar 2011, 01:07 PM
Hi Julien,

You can try this:

var groupToExpand = this.GridView.Items.Groups[0] as IGroup;
this.GridView.ExpandGroup(groupToExpand);

The list of groups is this.GridView.Items.Groups. They are of type IGroup. You can go down the tree through IGroup.Items when the group does not have subgroups (IGroup.HasSubgroups is false) or IGroup.Subgroups when the group has subgroups (when IGroup.HasSubgroups is true).

When you call ExpandGroup the grid will expand the group and all its parent groups if it is nested.

If you can find the correct group -- you will no longer need the ContainerFromItem -- simply call

this.GridView.ExpandGroup(groupToExpand);

I hope this helps.

Kind regards,
Ross
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Tingting
Top achievements
Rank 1
answered on 04 May 2012, 02:36 PM
Hi, 

  I have a RadGridView for just showing the data. 

 <telerik:RadGridView IsSynchronizedWithCurrentItem="true" RowIndicatorVisibility="Collapsed" IsFilteringAllowed="False" CanUserFreezeColumns="False" Name="dgrContratsPresto" ItemsSource="{Binding}" Grid.Row="1" Margin="60,0,60,0" RowHeight="30" Height="300"  HorizontalAlignment="Stretch" VerticalAlignment="Top" TabIndex="6" AutoGenerateColumns="False" ShowGroupPanel="False" FontFamily="SFR"  FontSize="14">
                    <telerik:RadGridView.Columns>
                        <telerik:GridViewDataColumn 
                        DataMemberBinding="{Binding Titu, Mode=OneWay}" UniqueName="nmatt" IsReadOnly="True"
                        Header="N° Titulaire" HeaderTextAlignment="Center" TextAlignment="Center"
                        Width="100" IsFilterable="True"/>
                        <telerik:GridViewDataColumn
                        DataMemberBinding="{Binding NomTitu, Mode=OneWay}" TextAlignment="Center"
                        Header="Nom Titulaire" HeaderTextAlignment="Center" Width="200"/>


                        <telerik:GridViewDataColumn
                        DataMemberBinding="{Binding NumeroContrat}" TextAlignment="Center"
                        Header="N° Contrat" HeaderTextAlignment="Center" Width="100"/>
                        <telerik:GridViewDataColumn
                        DataMemberBinding="{Binding TypeActe}" TextAlignment="Center"
                        Header="Type d'Acte" HeaderTextAlignment="Center" Width="300"/>
                        <telerik:GridViewDataColumn
                        DataMemberBinding="{Binding CodePostal}" TextAlignment="Center"
                        Header="CP" HeaderTextAlignment="Center" Width="60"/>
                        <telerik:GridViewDataColumn
                        DataMemberBinding="{Binding Ville}" TextAlignment="Center"
                        Header="Ville" HeaderTextAlignment="Center" Width="100"/>
                    </telerik:RadGridView.Columns>
                </telerik:RadGridView>

how can I set the GridView to go to the next row on TAB instead of the next cell. 
I have tried DefaultKeyboardCommandProvider, But it didn't work...  I think that it consider the next item is cell... 

0
Maya
Telerik team
answered on 04 May 2012, 02:50 PM
Hello,

Indeed, you can create your own custom keyboard command provider and update the commands that are executed on pressing Tab key. In you case you can invoke MoveDown command instead of MoveNext. 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Dempsey
Top achievements
Rank 1
answered on 11 Dec 2012, 04:26 AM
That was a great explanation way up there, Ross. More replies like that would surely prove help to us all. Too bad it turned out to be bogus advice! :-) At least that explains things on an unsorted, unfiltered grid. So thanks.
0
Sasireka
Top achievements
Rank 1
answered on 18 Jul 2013, 02:53 PM
Hi,

I have on telerik radgridview when i tried to open message box with Ok/cancel options while mouse double click of selected row. i couldnt able to select OK/Cancel options (The focus not set to the ok button). 

please help us to sort it out.

Regards,
Sasireka
0
Maya
Telerik team
answered on 19 Jul 2013, 08:56 AM
Hello Sasireka,

I have tried to reproduce the issue you reported, but witout much success. I am attaching the sample project that I used for a reference. 

Regards,
Maya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Sasireka
Top achievements
Rank 1
answered on 20 Jul 2013, 07:22 AM
Hi,
i am using un editable radgridview for that gridview how to enable to click  messagebox option while mouse double click
0
Maya
Telerik team
answered on 22 Jul 2013, 07:28 AM
Hi Sasireka,

The same event will be fired if the cell is in edit mode. If you want that message box to be displayed only when in edti mode, you can check the state of the cell:

private void OnCellDoubleClick(object sender, RadRoutedEventArgs e)
        {
            if ((e.OriginalSource as GridViewCell).IsInEditMode)
            {
                string messageBoxText = "Do you want to save changes?";
                string caption = "Word Processor";
                MessageBoxButton button = MessageBoxButton.YesNoCancel;
                MessageBoxImage icon = MessageBoxImage.Warning;
 
                MessageBox.Show(messageBoxText, caption, button, icon);
            }
        }
 

Regards,
Maya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
ivan
Top achievements
Rank 1
answered on 01 Oct 2013, 10:51 PM
Hi can you tell me what means objectToSelect ?

I have a datapager for paginate the radgridview.  I need that when changing page the first row get selected.


0
Lawrence
Top achievements
Rank 2
Iron
answered on 09 Jan 2015, 03:44 PM
I have subclassed the RadGridView and added this method, but you can easily turn this into an extension method.  This will select the your item on the appropriate page.  A couple of caveats - 1) Your grid must have all of the data loaded 2) You ItemSource must be of type Collection<T>.  Hope this helps.

public void ScrollIntoView<T>(T dataItem, RadDataPager radDataPager)
{
    var items = (Collection<T>)this.ItemsSource;
    int index = items.IndexOf(dataItem);
    int page = (int)Math.Floor((double)index / radDataPager.PageSize);
 
    radDataPager.MoveToPage(page);
 
    this.SelectedItem = items[index];
    this.ScrollIntoView(this.SelectedItem);
}
Tags
DataPager
Asked by
Julien
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Julien
Top achievements
Rank 1
Tingting
Top achievements
Rank 1
Maya
Telerik team
Dempsey
Top achievements
Rank 1
Sasireka
Top achievements
Rank 1
ivan
Top achievements
Rank 1
Lawrence
Top achievements
Rank 2
Iron
Share this question
or