Header Checkbox for all Columns

2 posts, 0 answers
  1. Chris
    Chris avatar
    1 posts
    Member since:
    May 2016

    Posted 29 Apr Link to this post

    I'm trying to add a checkbox to all column headers of a GridView, however I cannot see an easy way to accomplish it.  Basically, I have a dynamically generated set of columns (which each can contain a different editor).  I want a checkbox in the header cell which will allow me to enable/disable that column, however I cannot see a way to do it (I basically want to replicate the 'EnableHeaderCheckBox' functionality of the GridViewCheckBoxColumn, except on all columns regardless of data type).  Can anybody offer any advice on how this can be acheived?
  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3903 posts

    Posted 30 Apr Link to this post

    Hello, Chris,

    RadGridView provides a convenient way for creating custom cells. The possible solution that I can suggest is to create a custom header cell element and add a checkbox in it. Thus, when toggling its state you can perform the desired logic.

    I have prepared a sample code snippet for your reference:

            private void RadForm1_Load(object sender, EventArgs e)
            { 
                this.productsTableAdapter.Fill(this.nwindDataSet.Products);
                this.radGridView1.BestFitColumns();
    
                this.radGridView1.CellFormatting+=radGridView1_CellFormatting;
                this.radGridView1.Columns.RemoveAt(1);
    
                CustomColumn customColumn = new CustomColumn();
                customColumn.Width = 100;
                customColumn.Enabled = true;
                customColumn.FieldName = "ProductName";
                this.radGridView1.Columns.Insert(1,customColumn);
            }
    
            private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
            {
                if (e.Column.FieldName=="ProductName" && e.Row is GridViewDataRowInfo)
                {
                    e.CellElement.Enabled = ((CustomColumn)e.Column).Enabled;
                }
            }
    
            public class CustomColumn : GridViewTextBoxColumn
            {
                public override Type GetCellType(GridViewRowInfo row)
                {
                    if (row is GridViewTableHeaderRowInfo)
                    {
                        return typeof(CustomHeaderCell);
                    }
                    return base.GetCellType(row);
                }
    
                public bool Enabled { get; set; }
            }
    
            public class CustomHeaderCell : GridHeaderCellElement
            { 
                public CustomHeaderCell(GridViewColumn column, GridRowElement row) : base(column, row)
                {
                }
    
                protected override Type ThemeEffectiveType
                {
                    get
                    {
                        return typeof(GridHeaderCellElement);
                    }
                }
                public override bool IsCompatible(GridViewColumn data, object context)
                {
                    return data is CustomColumn;
                }
    
                RadCheckBoxElement checkBox = new RadCheckBoxElement();
    
                protected override void CreateChildElements()
                {
                    base.CreateChildElements();
                    this.checkBox.CheckStateChanged += checkBox_CheckStateChanged;
                    checkBox.StretchHorizontally = true;
                    this.Children.Add(checkBox);
                }
    
                private void checkBox_CheckStateChanged(object sender, EventArgs e)
                {
                    CustomColumn column = this.ColumnInfo as CustomColumn;
                    if (column != null)
                    {
                        column.Enabled = this.checkBox.Checked;
                        this.GridControl.MasterTemplate.Refresh();
                    }
                }
    
                protected override void SetContentCore(object value)
                {
                    base.SetContentCore(value);
                    this.DrawText = false;
                    this.checkBox.CheckStateChanged -= checkBox_CheckStateChanged;
                    CustomColumn column = this.ColumnInfo as CustomColumn;
                    if (column != null)
                    {
                        this.checkBox.Text = column.FieldName;
                        this.checkBox.Checked = column.Enabled ;
                    }
    
                    this.checkBox.CheckStateChanged += checkBox_CheckStateChanged;
                }
            }

    I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
Back to Top