Implementing "Select All" Checkbox

4 posts, 0 answers
  1. Support
    Support avatar
    9 posts
    Member since:
    Oct 2012

    Posted 20 Nov 2012 Link to this post

    Hi there,

    I am having some trouble implementing a "select all" checkbox for the RadGridView. I have successfully added checkboxes to group headers as shown in the attached screenshot. I need the ability to tick a box (which sits outside the grid) so it will select or deselect all items in the grid. 

    Currently when I click on the "select all" checkbox, only four items are selected in the grid out of 131. I believe this has something to do with the control reusing cells. I have implemented the following class.

    public class CustomGroupCellHeader : GridGroupContentCellElement
        {
            public event EventHandler CheckChanged;
            public RadCheckBoxElement Checkbox { get; set; }
            public GridViewColumn Column { get; set; }
            public GridRowElement Row { get; set; }
            public Guid OperatorId { get; set; }
            public string OperatorName { get; set; }
     
            public CustomGroupCellHeader(GridViewColumn column, GridRowElement row) :
                base(column, row)
            {
                this.Column = column;
                this.Row = row;
            }
     
            protected override void CreateChildElements()
            {
                base.CreateChildElements();
                this.Checkbox = new RadCheckBoxElement();
                this.Checkbox.MinSize = new Size(10, 10);
                this.Checkbox.ToggleStateChanged += new StateChangedEventHandler(_checkbox_ToggleStateChanged);
                this.Children.Insert(0, this.Checkbox);
            }
     
            protected override System.Drawing.SizeF ArrangeOverride(System.Drawing.SizeF finalSize)
            {
                SizeF size = base.ArrangeOverride(finalSize);
                RectangleF rect = GetClientRectangle(finalSize);
                if (this.Checkbox != null)
                {
                    this.Checkbox.Arrange(new RectangleF(rect.X, rect.Y + 5, 10, 10));
                }
     
                return size;
            }
     
            private void _checkbox_ToggleStateChanged(object sender, StateChangedEventArgs args)
            {
                if (this.CheckChanged != null)
                {
                    this.CheckChanged(this, null);
                }
            }

    I have also added the following code into the CreateCell event handler:

    private List<CustomGroupCellHeader> _groupCheckboxes = new List<CustomGroupCellHeader>();
    private void results_CreateCell(object sender, GridViewCreateCellEventArgs e)
    {
        if (e.CellType == typeof(GridGroupContentCellElement))
        {
            CustomGroupCellHeader customCell = new CustomGroupCellHeader(e.Column, e.Row);
            customCell.CheckChanged += new EventHandler(customCell_CheckChanged);
            e.CellElement = customCell;
     
            _groupCheckboxes.Add(customCell);
        }
    }

    The variable _groupCheckboxes is intended to store the checkboxes for group headers. How can I ensure that this variable stores ALL checkboxes for group headers?

    Thanks
  2. Svett
    Admin
    Svett avatar
    728 posts

    Posted 22 Nov 2012 Link to this post

    Hello Magnetism,

    It seems that the implementation of the custom group cell element is correct. Nevertheless, I cannot determine the implementation the CheckChanged event handler. Could you share a code snippet that shows its actual implementation? Meanwhile, you can select all child rows of a particular group row by using the following code snippet in your custom cell:

    private void _checkbox_ToggleStateChanged(object sender, StateChangedEventArgs args)
    {
        GridViewGroupRowInfo groupRow = this.RowInfo as GridViewGroupRowInfo;
        bool isSelected = args.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On;
        foreach (GridViewRowInfo row in groupRow.ChildRows)
        {
            row.IsSelected = isSelected;
        }
    }

    All the best,
    Svett
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Nomesh
    Nomesh avatar
    1 posts
    Member since:
    Oct 2016

    Posted 18 Oct in reply to Svett Link to this post

    Hi Team,

    I am working on Multilevel Gridview in winforms( 4 Levels)

    I have Select All Header Checkbox in the GrdiviewCheckBoxColumn in all the four levels.

    My requirement is,

    When I check/Uncheck the Header Checkbox in one of the levels, I need to change the toggle state of the Header checkboxes automatically in the remaining levels.

    For Eg..  If I check the HeaderCheckbox in first level, all the HeaderCheckboxes in the second, third and fourth levels should be checked automatically

    If I check the HeaderCheckbox in second level, all the HeaderCheckboxes in the third and fourth levels should be checked automatically.

     

    Thanks,

    Nomesh

  5. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 19 Oct Link to this post

    Hello Nomesh,

    Thank you for writing.  

    Note that since Q2 2014 SP1, RadGridView supports header checkbox in the GridViewCheckBoxColumn. It is just necessary to set the GridViewCheckBoxColumn.EnableHeaderCheckBox property to true. However, to check/uncheck the inner levels, it is necessary to do it programmatically. You can handle the RadGridView.HeaderCellToggleStateChanged event and iterate all the inner templates and their Rows collection and update the respective boolean cell value.
     
    As to the specific custom implementation for the header checkbox, please refer to the attached sample project which result is illustrated in the attached gif file. Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best.
     
    I hope this information helps. Should you have further questions I would be glad to help.

     Regards,
    Dess
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top