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
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
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.
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
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
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;
}
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.
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
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
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
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
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
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
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
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
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
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
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...
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.
Maya
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
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
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.
Maya
Telerik
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 >>
Hi,
i am using un editable radgridview for that gridview how to enable to click messagebox option while mouse double click
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
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 >>
objectToSelect
?I have a datapager for paginate the radgridview. I need that when changing page the first row get selected.
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);
}