Grouped datagrid, find a specific groupheader

7 posts, 0 answers
  1. Ravi
    Ravi avatar
    104 posts
    Member since:
    Oct 2012

    Posted 18 Aug 2011 Link to this post

    <telerikGrid:RadGridView.GroupHeaderTemplate>
                            <DataTemplate>
                                <StackPanel x:Name="stkPanel" Orientation="Horizontal">
      
                                    <Button x:Name="headerCheckButton" Margin="0,0,0,0" Style="{Binding Group, Converter={StaticResource WrntyAFRGroupHeaderConverter}, ConverterParameter=ISCHECKED}" Click="headerCheckButton_Click"/>
      
                                    <TextBlock x:Name="tbItemClass"  Margin="2,0,50,0" Width="150" Text="{Binding Group, Converter={StaticResource WrntyAFRGroupHeaderConverter}, ConverterParameter=ITEMCLASS}"/>
      
                                    <Button x:Name="btnNotes" Margin="50,0,25,0" Width="35" 
                                            Style="{Binding Group, Converter={StaticResource WrntyAFRGroupHeaderConverter},ConverterParameter=NOTES}" 
                                            Tag="{Binding Group, Converter={StaticResource WrntyAFRGroupHeaderConverter}, ConverterParameter=NOTESOBJECT}"
                                            Click="btnNotes_Click"/>
                                </StackPanel>
                            </DataTemplate>
                        </telerikGrid:RadGridView.GroupHeaderTemplate>
    Hi,

    I have a radgrid view implemented with grouping enabled. I have the value of the grouping column, what I want is to return that row based on the group name I pass.

    Why i want the group header is, I have few other controls on the row on which i want to set a style for a button based on some condition at runtime. foreach group i want to access the btnNotes and runtime based on some condition.

    I am using 2010.1.422.1030 version. please suggest ONLY a solution for this version.

    Let me know if you require any further information on this.

    Thanks,
    Shiras
  2. Ravi
    Ravi avatar
    104 posts
    Member since:
    Oct 2012

    Posted 19 Aug 2011 Link to this post

    Hoping today to get a response on this.

    Thanks,
    Shiras
  3. DevCraft banner
  4. Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 19 Aug 2011 Link to this post

    Hello David,

     

    If you want to apply a style based on a property value it is much more appropriate to work directly with the items. You may return the corresponding style with a simple converter based on the key of each group:
    The snippet below demonstrates how this can be achieved:

    MainPage.xaml:

    <UserControl
        xmlns:local="clr-namespace:SilverlightApplication572"
        x:Class="SilverlightApplication572.MainPage">
        <UserControl.Resources>
            <local:ValueToColorConverter x:Key="con"/>
            </UserControl.Resources>
            <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
            <telerik:RadGridView   ItemsSource="{Binding Collection}">
            <telerik:RadGridView.GroupHeaderTemplate>
                <DataTemplate>
                    <telerik:RadButton Width="100" Height="25" Style="{Binding Group.Key,Converter={StaticResource con}}"  Content="{Binding Group.Key}"/>
                    </DataTemplate>
                </telerik:RadGridView.GroupHeaderTemplate>
                </telerik:RadGridView>     
        </Grid>
    </UserControl>

    ValueToColorConverter.cs:

    public class ValueToColorConverter : IValueConverter
       {
     
           object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
           {
               var resultStyle = new Style() { TargetType = typeof(RadButton) };
               var item = ((string)value);
               if (item.Equals("Aenean cras duis mauris"))
               {
                   resultStyle.Setters.Add(new Setter(RadButton.BackgroundProperty, new SolidColorBrush(Colors.Red)));
               }
               else
               {
                   resultStyle.Setters.Add(new Setter(RadButton.BackgroundProperty, new SolidColorBrush(Colors.Green)));
               }
     
               return resultStyle;
           }
     
           object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
           {
               throw new NotImplementedException();
           }
       }


    Feel free to change this in the way you need. 


    Regards,
    Vanya Pavlova
    the Telerik team

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

  5. Ravi
    Ravi avatar
    104 posts
    Member since:
    Oct 2012

    Posted 19 Aug 2011 Link to this post

    Value converter is already written and is working as expected.

    What i am trying to do is, in the GroupHeaderTemplate, I have a datatemplate which has a button. So after the grid is loaded, based on the user action in the screen, the button style need to be applied or removed based on the condition, to apply the sytle i will have to get the control button of that particular group and then apply the style using SetBinding.
     
    Here i am unable to get the group by the Key and then the GroupHeaderTemplate -> button.

    Hope this is clear.
  6. Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 19 Aug 2011 Link to this post

    Hi David,

     

    The GroupHeaderTemplate is a simple DataTemplate. Once you set the DataTemplate, you may access the buttons in each template by name using the extension method ChildrenOfType. Then just check its content and perform some action:

    MainPage.xaml:

    <telerik:RadGridView x:Name="radGridView" ItemsSource="{Binding Collection}">
            <telerik:RadGridView.GroupHeaderTemplate>
                <DataTemplate>
                    <telerik:RadButton x:Name="button1" Width="100" Height="25" Content="{Binding Group.Key}"/>
                    </DataTemplate>
                </telerik:RadGridView.GroupHeaderTemplate>
    </telerik:RadGridView>     


    MainPage.xaml.cs:

    var buttons = this.radGridView.ChildrenOfType<RadButton>().Where(b => b.Name == "button1").ToList();
              foreach (var btn in buttons)
              {
                  if (btn.Content.Equals("Aenean cras duis mauris"))
                  {
                      MessageBox.Show(btn.Content.ToString());
                  }
              }


    Please let me know how this works for you!


    Best wishes,
    Vanya Pavlova
    the Telerik team

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

  7. Ravi
    Ravi avatar
    104 posts
    Member since:
    Oct 2012

    Posted 19 Aug 2011 Link to this post

    please find the attached with comments.
  8. Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 19 Aug 2011 Link to this post

    Hi David,

     

    As I suggested in my previous post you should work with the data item collection rather than the groups. 
    Just access the underlying business object through the Items collection of RadGridView and the property ADLM and check its values. For example: if the value is 1 find the button named "Notes" from the DataTemplate and change it in a way using the extension method ChildrenOfType as demonstrated in my previous reply.

    Similar approach is used in our online template selector demo:
    "GroupHeaderTemplateSelector".


    Best wishes,
    Vanya Pavlova
    the Telerik team

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

Back to Top
DevCraft banner