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

Getting row when grid is grouped fails

10 Answers 110 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dominik
Top achievements
Rank 1
Dominik asked on 02 Sep 2011, 03:13 PM
Hi

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

Sort by
0
Maya
Telerik team
answered on 03 Sep 2011, 08:59 AM
Hi Dominik,

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 >>

0
Dominik
Top achievements
Rank 1
answered on 05 Sep 2011, 08:09 AM
Hi

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?
0
Maya
Telerik team
answered on 05 Sep 2011, 10:24 AM
Hello Dominik,

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 ? 
 

Greetings,
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 >>

0
Dominik
Top achievements
Rank 1
answered on 05 Sep 2011, 02:23 PM
Hi

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
0
Maya
Telerik team
answered on 05 Sep 2011, 02:37 PM
Hi Dominik,

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.
 

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 >>

0
Dominik
Top achievements
Rank 1
answered on 06 Sep 2011, 03:13 PM

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.


0
Maya
Telerik team
answered on 07 Sep 2011, 10:07 AM
Hi Dominik,

RowStyleSelector will be invoked on PropertyChanged. So, you need to make sure this event is fired, if you want the styles to be reapplied. 
 

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 >>

0
Yves
Top achievements
Rank 1
answered on 08 Aug 2012, 10:11 AM
Dear Telerik support,

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.
0
Maya
Telerik team
answered on 08 Aug 2012, 02:08 PM
Hi Menze,

Please take a look at this forum thread illustrating how you can expand all details. 

All the best,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Yves
Top achievements
Rank 1
answered on 08 Aug 2012, 03:03 PM
Maya,

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. 
Tags
GridView
Asked by
Dominik
Top achievements
Rank 1
Answers by
Maya
Telerik team
Dominik
Top achievements
Rank 1
Yves
Top achievements
Rank 1
Share this question
or