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

[Solved] Group Checkbox working with GridViewSelectColumn (Q3)

2 Answers 289 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.
Sandi
Top achievements
Rank 1
Sandi asked on 12 Dec 2009, 04:00 PM
I am wondering if someone out there has some suggestions on how we can get our GridViewSelectColumn 100% working with the group checkboxes that we have added to our grid. It is working with the exception that if the particular group has rows that are not able to fit on the screen then they are not being selected.

The problem code is in here...even though if there are 100 rows in the group only those rows that are visible on the screen get checked because those that are not visible do not have a GridViewRow...

 var row = groupRow.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow winds up empty...

public static void CheckUncheckRows(bool doCheck, GridViewGroupRow groupRow) 
        { 
 
            if (!_IsInSelectAllMode) 
            { 
                foreach (var item in groupRow.Items) 
                { 
                    var row = groupRow.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow; 
                    if (row != null
                    { 
                        row.IsSelected = doCheck; 
                    } 
                } 
            } 
        } 


Since I cannot post the solution here...these are the different parts that allow it to work

The Grid xaml

<grid:RadGridView Grid.Row="2"  x:Name="FeatureAttributeDataGrid"   
                                          Margin="0"                        
                                          MultipleSelect="True"                                            
                                          AutoGenerateColumns="False"  
                                          AutoExpandGroups="True"  
                                          ColumnsWidthMode="Auto"  
                                          CanUserFreezeColumns="True"                                            
                                          BorderThickness="0"  > 
            <grid:RadGridView.GroupHeaderTemplate> 
                <DataTemplate> 
                    <StackPanel Orientation="Horizontal" > 
                        <local:GroupCheckBox /> 
                        <TextBlock Text="{Binding Header}"/> 
                    </StackPanel> 
                </DataTemplate> 
            </grid:RadGridView.GroupHeaderTemplate> 
        </grid:RadGridView> 

The GroupCheckBox

<UserControl x:Class="TestGrouping.GroupCheckBox" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    Width="16" Height="16"
    <Grid x:Name="LayoutRoot" Background="White"
        <CheckBox x:Name="PART_CheckBox" Click="PART_CheckBox_Click" /> 
    </Grid> 
</UserControl> 

The GroupCheckBox C# code

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Telerik.Windows.Controls; 
using Telerik.Windows.Controls.GridView; 
using Telerik.Windows.Data; 
 
namespace TestGrouping 
    public partial class GroupCheckBox : UserControl 
    { 
        public GroupCheckBox() 
        { 
            InitializeComponent(); 
        } 
 
        private void PART_CheckBox_Click(object sender, RoutedEventArgs e) 
        { 
            CheckBox chk = (CheckBox)sender; 
            GridViewGroupRow groupRow = chk.ParentOfType<GridViewGroupRow>(); 
 
            MainGrid.CheckUncheckRows((bool)chk.IsChecked, groupRow); 
 
 
            IList<GridViewGroupRow> grpRows = groupRow.ChildrenOfType<GridViewGroupRow>(); 
            MainGrid.CheckUncheckGroupRows((bool)chk.IsChecked, grpRows); 
        }   
        
    } 
 




The Grid and Methods

 public MainGrid() 
        {             
 
this.FeatureAttributeDataGrid.AddHandler(GridViewCell.MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnGridMouseLeftButtonUp), true);            
        } 
 
public static void CheckUncheckGroupRows(bool doCheck, IList<GridViewGroupRow> grpRows) 
        { 
            for (int i = 0; i < grpRows.Count; i++) 
            { 
                GridViewGroupRow grpRowChild = grpRows[i]; 
                IList<CheckBox> lstChk = grpRowChild.ChildrenOfType<CheckBox>(); 
 
                lstChk[0].IsChecked = doCheck; 
 
                CheckUncheckRows(doCheck, grpRowChild); 
            } 
        } 
 
        public static void CheckUncheckRows(bool doCheck, GridViewGroupRow groupRow) 
        { 
 
            if (!_IsInSelectAllMode) 
            { 
                foreach (var item in groupRow.Items) 
                { 
                    var row = groupRow.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow; 
                    if (row != null
                    { 
                        row.IsSelected = doCheck; 
                    } 
                } 
            } 
        } 
 
        private void OnGridMouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
        { 
            _IsInSelectAllMode = true
            var senderElement = (FrameworkElement)e.OriginalSource; 
            var clickedCell = senderElement.ParentOfType<GridViewHeaderCell>(); 
 
            if (clickedCell != null
            { 
                CheckBox chkHeader = clickedCell.Column.Header as CheckBox; 
                if (chkHeader != null
                { 
                    IList<GridViewGroupRow> grpRows = FeatureAttributeDataGrid.ChildrenOfType<GridViewGroupRow>(); 
                    CheckUncheckGroupRows((bool)chkHeader.IsChecked, grpRows); 
 
                } 
            } 
            _IsInSelectAllMode = false
        } 


Anybody have any ideas on how I can make this work while still having the full functionality of the GridViewSelectColumn?

I will attach my

I appreciate any suggestions.

Thanks
Sandi

2 Answers, 1 is accepted

Sort by
0
Accepted
Milan
Telerik team
answered on 14 Dec 2009, 01:54 PM

Hello Sandi,

You can try working with data items instead of rows:

public static void CheckUncheckRows(bool doCheck, GridViewGroupRow groupRow)
{
    var parentGrid = groupRow.ParentOfType<GridViewDataControl>();
  
    if (!_IsInSelectAllMode)
    {
        foreach (var item in groupRow.Items)
        {
            if (doCheck)
                parentGrid.SelectedItems.Add(item);
            else
                parentGrid.SelectedItems.Remove(item);
        }
    }
}

 


Sincerely yours,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Sandi
Top achievements
Rank 1
answered on 14 Dec 2009, 09:42 PM
This is perfect...
Thanks for your help.

Sandi
Tags
GridView
Asked by
Sandi
Top achievements
Rank 1
Answers by
Milan
Telerik team
Sandi
Top achievements
Rank 1
Share this question
or