selected row when grouping

8 posts, 0 answers
  1. Zbigniew Kozłowski
    Zbigniew Kozłowski avatar
    60 posts
    Member since:
    Apr 2010

    Posted 01 Oct 2010 Link to this post

    Hi,
    i got a problem with getting selected row when grid is groupped. I got event on 
    GridView.RowDetailsProvider.PropertyChanged
    than in void:
    var selectedRow = (GridViewRow)this.ListGridView.ItemContainerGenerator.ContainerFromItem(this.ListGridView.SelectedItem);
    but it works only when grid is not groupped. How can i get GridViewRow from group?

  2. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 04 Oct 2010 Link to this post

    Hello Zbigniew Kozłowski,

    Could you please provide us with more information about your scenario? It might be possible to achieve the goal without even retrieving a reference to a UI row. I am interested to know why it is necessary to access UI rows?

    Sincerely yours,
    Milan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Zbigniew Kozłowski
    Zbigniew Kozłowski avatar
    60 posts
    Member since:
    Apr 2010

    Posted 04 Oct 2010 Link to this post

    well i need two things, one is to get object, but i could achive it without a reference to UI, but second thing is to change font weight of current row
  5. Zbigniew Kozłowski
    Zbigniew Kozłowski avatar
    60 posts
    Member since:
    Apr 2010

    Posted 06 Oct 2010 Link to this post

    so anyone can help me ?
  6. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 07 Oct 2010 Link to this post

    Hi Zbigniew Kozłowski,

    You can easily bind the FontWeight property of the row to its IsSelected or IsCurrent property. You could try the following approach:

    <my:BoolToFintWeightConverter x:Key="converter"/>
      
    <Style TargetType="telerik:GridViewRow" x:Key="rowStyle">
        <Setter Property="FontWeight" 
                Value="{Binding IsSelected, 
            RelativeSource={RelativeSource Self}, 
            Converter={StaticResource converter}}"/>
    </Style>

    public class BoolToFintWeightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var boolValue = (bool) value;
      
            if (boolValue)
            {
                return FontWeights.Bold;
            }
            else
            {
                return FontWeights.Normal;
            }
        }
      
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    The last step is to assign the custom row style to the RowStyle property of the grid.

    Hope this helps.


    Greetings,
    Milan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Steve
    Steve avatar
    124 posts
    Member since:
    Jun 2011

    Posted 13 May 2014 Link to this post

    Milan,
    It really doesn't matter why one would want to get the GridViewRow, how can you get it?

    ​var selectedRow = (GridViewRow)this.ListGridView.ItemContainerGenerator.ContainerFromItem(this.ListGridView.SelectedItem);
    but it works only when grid is not groupped. How can i get GridViewRow from group?

    And why does the above return null?

    Thanks ahead of time,
    Steve


  8. Boris
    Admin
    Boris avatar
    276 posts

    Posted 14 May 2014 Link to this post

    Hello Steve,

    One possible reason for the ItemContainerGenerator.ContainerFromItem() method to stop working is for the SelectedItem to be null. You need a SelectedItem otherwise you won't get its container.

    Another way to get the grouped row is to check if there are any groups and then use the ChildrenOfType<GridViewRow>() extension method on the GridView to get the selected row container. However, this will only work if the GroupRenderMode is set to Nested (this is the default mode), if its set to Flat you can use the ItemContainerGenerator.ContainerFromItem(grid.SelectedItem) method to get the selected container row container. 

    private void Button1_Click(object sender, RoutedEventArgs e)
            {
                var grid = this.clubsGrid as RadGridView;
     
                if (grid.GroupCount > 0 && grid.SelectedItem != null)
                {
                    if (grid.GroupRenderMode == GroupRenderMode.Nested)
                    {
                        var groupedRows = grid.ChildrenOfType<GridViewRow>().FirstOrDefault(x => x.IsSelected);                
                    }
                    else
                    {
                        // If GroupRenderMode="Flat" use this
                        var rowContainer = grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem) as GridViewRow;
                    }
                }
            }

    For more information about ChildrenOfType() method you can check Easy programmatic UI customization for WPF and Silverlight blog post.

    In order to advise you better could you please provide us with more details about your scenario ?

    We are looking forward to your response.


    Regards,
    Boris Penev
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  9. Steve
    Steve avatar
    124 posts
    Member since:
    Jun 2011

    Posted 14 May 2014 in reply to Boris Link to this post

    Apparently the key was the GroupRenderMode!
    Thanks for the quick response,
    Steve
Back to Top
UI for WPF is Visual Studio 2017 Ready