In our silverlight 4 application, we have encountered a problem when the grid is grouped and we want to select a row:
public
void
PerformFullCheck()
{
for
(
int
i = 0; i <
this
.DataGrid.Items.Count; i++)
{
var item = DataGrid.Items[i];
var rowItem = DataGrid.ItemContainerGenerator.ContainerFromItem(item);
var row = rowItem
as
GridViewRow;
var toEnable = ItemToBeEnabled(item);
if
(row!=
null
){
// when grid is grouped-> row is always null :-(
row.IsEnabled = toEnable;
}
}
}
When the grid is grouped, the ItemContainerGenerator always returns null and we cannot enable/disable the row depending on some (rather complicated) right checks?
Is there a solution to this?
Greetings and thanks in advance
10 Answers, 1 is accepted
I would recommend you to work directly with the data items and set the SelectedItem property of the grid to the item you want to be selected. For example:
for (int i = 0; i < this.playersGrid.Items.Count; i++)
{
var item = this.playersGrid.Items[i];
//Perform the logic you require for choosing the item to be selected.
this.playersGrid.SelectedItem = item;
}
Regards,
Maya
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>
There must have been a missunderstanding. I dont want to select a certain row, but I have some logic to enable/disable a row. This works with the pasted code when the grid is not (!) grouped. As soon as the grid is grouped, i have no way to get to the row.
How can i get the rows (GridViewRow) when the grid is grouped?
Would you please share a bit more information about the scenario you want to accomplish ? Do you want to define a style for particular rows no matter if the grid is grouped or not ? Or do you want to define a row style only when the grid is grouped ?
Maya
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>
I want to enable/disable the row depending on some rules that are applied to the business object that is bound to the grid.
A row that is disabled:
- cannot be edited
- cannot be selected (just readonly, this would be possible)
- should be visible as disabled
There is a property "IsEnableD" on the GridViewRow class that I use successfully when the grid is not grouped. However, when the grid is grouped, i never get to the instance of a GridViewRow class (as described in previous posts)
Thanks for your help
In this case you would better use a RowStyleSelector. Please take a look at our online documentation and demos for a reference. Once you define this RowStyleSelector for the grid, the styles will be applied no matter if the grid is grouped or not.
Maya
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>
Hi
Thanks a lot for your help. This works well and is a lot cleaner than what i tried to do.. However, i have one more question:
In our application we display the same instance of a grid in various Content controls. Example: The grid is sometimes shown on the bottom and sometimes at the top, depending on the users preferences. It is crucial, that it is the same instance of the grid as the column settings (filtering, ordering etc) must remain the same. Furthermore, depending on the position the grid is readonly or in edit mode. Therefore, whenever we put the grid in a different content control, the style selector checks must be performed again for all visible rows.
Example Code:
public
class
GridCheckStyle : StyleSelector
{
public
override
Style SelectStyle(
object
item, DependencyObject container)
{
var row = container
as
GridViewRow;
var grid = row.GridViewDataControl;
var anf = item
as
Anforderung;
if
(anf !=
null
&& !grid.IsReadOnly)
{
if
(!anf.CanUserModify)
{
return
DisabledStyle;
}
}
return
null
;
}
}
----------------------------------------------
public
class
UserControlWithGrid: UserControl{
private
void
UserControl_Unloaded(
object
sender, System.Windows.RoutedEventArgs e)
{
// ensure that style selector is refreshed for every visible row
this
.DataGrid.Rebind();
//works but reloads everything
// this.DataGrid.ResetTheme(); // does not work
// this.DataGrid.ApplyTemplate(); // does not work
// this.DataGrid.UpdateLayout(); // does not work
}
To solve this issue, I just Rebind all the items. However, this triggers an unnecessary server request which costs quite a lot of time.
Is there a way to avoid that unnecessary request (triggerred by Datarid.Rebind())? I just need a way to perform all the style select on all visible rows via code behind.
Thanks in advance.
RowStyleSelector will be invoked on PropertyChanged. So, you need to make sure this event is fired, if you want the styles to be reapplied.
Maya
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>
I am trying to do a similar thing, but in my case I want to expand/collapse all row details.
I had the same problem with grouping and the ItemContainerGenerator.
Based on this thread, I made a style selector with collapsed and expanded style, but it throws exceptions 'Given key not found in dictionary'. If instead of DetailsVisibility, I use Background, it works fine.
Code:
<Grid.Resources> <l:CollapseExpandRowStyle x:Key="CollapseExpandRowStyle"> <l:CollapseExpandRowStyle.CollapsedStyle> <Style TargetType="telerik:GridViewRow"> <!--<Setter Property="DetailsVisibility" Value="Collapsed"/>--> <Setter Property="Background" Value="AliceBlue"/> </Style> </l:CollapseExpandRowStyle.CollapsedStyle> <l:CollapseExpandRowStyle.ExpandedStyle> <Style TargetType="telerik:GridViewRow"> <!--<Setter Property="DetailsVisibility" Value="Visible" />--> <Setter Property="Background" Value="BlanchedAlmond"/> </Style> </l:CollapseExpandRowStyle.ExpandedStyle> </l:CollapseExpandRowStyle> </Grid.Resources>
This code with the Background property works fine, but when I use the outcommented lines for the DetailsVisibility, the exception is thrown.
Alternatively, is there any way to get all rows in a group as GridViewRows, so we could make the original code work:
private void SetDetailsVisibilityToAllRows(Visibility visibility) { int i = 0; foreach (object item in this.DataControl.Items) { GridViewRow row = this.DataControl.ItemContainerGenerator.ContainerFromIndex(i) as GridViewRow; if (row != null) { row.DetailsVisibility = visibility; } i++; } }This code works if there is no grouping, but fails when grouping exists.
Thank you for your time.
Please take a look at this forum thread illustrating how you can expand all details.
Maya
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
Thank you. I managed to get it working with the example from the other post.
The method ChildrenOfType<GridViewRow> was what I was missing:
var rows = this.DataControl.ChildrenOfType<GridViewRow>(); Best regards.