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

Checkbox column in Grid

3 Answers 381 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Pinaki Basu
Top achievements
Rank 1
Pinaki Basu asked on 06 Oct 2010, 07:03 AM
Hi,

I have a hierarchical gridview with checkbox column. I have a checkbox in header as well.
When the header checkbox is checked/unchecked all other rows gets checked/unchecked accordingly.
But when i do the same in Child it is not working properly.
When i check/uncheck header of Child then the child rows of only expanded row should get checked/unchecked but not all the child rows.
Right now its applying changes for all child rows but i want it only for expanded row.

Below is my code:
<
private void AddCheckColumn(fc_rad_dataGridView dataGridView)
        {
            CustomCheckBoxColumn checkColumn = new CustomCheckBoxColumn();
            checkColumn.Name = "IsActive";
            checkColumn.FieldName = "Active";
            if (!dataGridView.Columns.Contains("IsActive"))
                dataGridView.Columns.Insert(dataGridView.Columns.Count - 1, checkColumn);
        }

public class CustomCheckBoxColumn : GridViewCheckBoxColumn
    {
        public override Type GetCellType(GridViewRowInfo row)
        {
            if (row is GridViewTableHeaderRowInfo)
            {
                return typeof(CheckBoxHeaderCell);
            }
            return base.GetCellType(row);
        }
    }

    public class CheckBoxHeaderCell : GridHeaderCellElement
    {
        RadCheckBoxElement checkbox;

        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(GridHeaderCellElement);
            }
        }

        public CheckBoxHeaderCell(GridViewColumn column, GridRowElement row)
            : base(column, row)
        {

        }

        public override void Initialize(GridViewColumn column, GridRowElement row)
        {
            base.Initialize(column, row);
            column.AllowSort = false;
            column.TextAlignment = ContentAlignment.MiddleRight;
        }

        public override void SetContent()
        {
        }

        protected override void DisposeManagedResources()
        {
            checkbox.ToggleStateChanged -= new StateChangedEventHandler(checkbox_ToggleStateChanged);
            base.DisposeManagedResources();
        }

        protected override void CreateChildElements()
        {
            base.CreateChildElements();
            checkbox = new RadCheckBoxElement();
            checkbox.ToggleStateChanged += new StateChangedEventHandler(checkbox_ToggleStateChanged);
            this.Children.Add(checkbox);
        }

        protected override SizeF ArrangeOverride(SizeF finalSize)
        {
            SizeF size = base.ArrangeOverride(finalSize);

            RectangleF rect = GetClientRectangle(finalSize);
            this.checkbox.Arrange(new RectangleF((finalSize.Width - this.checkbox.DesiredSize.Width) / 2, (rect.Height - 20) / 2, 20, 20));

            return size;
        }

        public override bool IsCompatible(GridViewColumn data, object context)
        {
            return data.Name == "IsActive" && context is GridTableHeaderRowElement
                && base.IsCompatible(data, context);
        }

        private void checkbox_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {  
            if (this.ColumnInfo is GridViewCheckBoxColumn)
            {
                bool valueState = false;

                if (args.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
                {
                    valueState = true;
                }
                for (int i = 0; i < this.ViewTemplate.Rows.Count; i++)
                {
                    this.ViewTemplate.Rows[i].Cells[this.ColumnIndex].Value = valueState;
                }
            }
        }
    }
>

Thanks,
Pinaki

3 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 06 Oct 2010, 07:56 AM
Hi,

The reason why this 'appears' not to be working is because the rows under each parent row is one child template, and therefore one grid. It behaves in the same way as the top level.
In order to do this you'll need to programatically check all of those child nodes that have a common property (such as the same parent Id). This would be dont by finding out which level you are clicking on and then looping through the rows to check the right ones.
I'll see if I can put a quick sample together for you when I have access to a development system
Hope that helps
Richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 06 Oct 2010, 01:27 PM
Hello Pinaki,

This thread (how to use a checkbox in a hierarchy grid as select all? ) might be of assistance, and also you might want to use the feature described in this article in order to hide / show collapse sign in rows that do not have any children.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Svett
Telerik team
answered on 11 Oct 2010, 03:54 PM
Hi Pinaki Basu,

You should iterate through the rows of the GridViewInfo instance which is the logical representation of each view in hierarchy. The instance of GridViewTemplate contains all rows of a concrete level. You should change the checkbox_ToggleStateChanged event handler:

private void checkbox_ToggleStateChanged(object sender, StateChangedEventArgs args)
{
    if (this.ColumnInfo is GridViewCheckBoxColumn)
    {
        bool valueState = false;
 
        if (args.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
        {
            valueState = true;
        }
        for (int i = 0; i < this.ViewInfo.Rows.Count; i++)
        {
            this.ViewInfo.Rows[i].Cells[this.ColumnIndex].Value = valueState;
        }
    }
}

I hope this helps.

Regards,
Svett
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
Tags
GridView
Asked by
Pinaki Basu
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Emanuel Varga
Top achievements
Rank 1
Svett
Telerik team
Share this question
or