GridHeaderCellElement in C++

4 posts, 0 answers
  1. John
    John avatar
    2 posts
    Member since:
    Feb 2012

    Posted 10 Feb 2012 Link to this post

    I am using C++/CLI with the Telerik GridView.  I have a checkbox column which I wanted to add the CheckBoxHeaderCell class to.  (Reference: http://www.telerik.com/support/kb/winforms/gridview/add-check-uncheck-all-check-box-in-the-header-cell.aspx).  I wrote the example and compiled it in C++/CLI from the given C# code.  However, I do not know how to tell my grid view to USE this class as a columns header cell.  Any help would be appreciated!

    - John


  2. Stefan
    Admin
    Stefan avatar
    2908 posts

    Posted 13 Feb 2012 Link to this post

    Hi John,

    Thank you for writing.

    I can provide you with help in C# and VB, but not in C++. As you can see in the example project attached in the KB article, after the column class is created, you can simply create an instance of the class and add the instantiated column to the Columns collection of RadGridView
    CustomCheckBoxColumn checkColumn = new CustomCheckBoxColumn();
    checkColumn.Name = "Select";
    checkColumn.HeaderText = "All";
    this.radGridView1.Columns.Insert(0, checkColumn);

    I hope that you find this information helpful.

    Kind regards,
    Stefan
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. John
    John avatar
    2 posts
    Member since:
    Feb 2012

    Posted 14 Feb 2012 Link to this post

    For my C++ brethren out there...here is the check box header cell in C++:

    public ref class CheckBoxHeaderCell : public GridHeaderCellElement
    {
        public: CheckBoxHeaderCell(GridViewColumn^ column, GridRowElement^ row) : GridHeaderCellElement(column, row)
        {
            InitializeComponent();
     
            this->_suspendProcessingToggleStateChanged = false;
     
            this->_checkbox = gcnew RadCheckBoxElement();
            this->_checkbox->ToggleStateChanged += gcnew StateChangedEventHandler(this, &CheckBoxHeaderCell::checkbox_ToggleStateChanged);
            this->Children->Add(this->_checkbox);
        }
     
        protected: ~CheckBoxHeaderCell()
        {
            if (components)
            {
                delete components;
            }
        }
     
        protected: virtual SizeF ArrangeOverride(SizeF finalSize) override
        {
            SizeF size = this->GridHeaderCellElement::ArrangeOverride(finalSize);
     
            RectangleF^ rect = GetClientRectangle(finalSize);
            this->_checkbox->Arrange(RectangleF((finalSize.Width - this->_checkbox->DesiredSize.Width) / 2, (rect->Height - 20) / 2, 20, 20));
     
            return size;
        }
     
        public: virtual bool IsCompatible(GridViewColumn^ data, System::Object^ context) override
        {
            return data->Name == "Select" && (dynamic_cast<GridTableHeaderRowElement^>(context) != nullptr) && this->GridHeaderCellElement::IsCompatible(data, context);
        }
     
        protected: void checkbox_ToggleStateChanged(System::Object^ sender, StateChangedEventArgs^ args)
        {
            if(!this->_suspendProcessingToggleStateChanged)
            {
                bool valueState = false;
     
                if(args->ToggleState == Telerik::WinControls::Enumerations::ToggleState::On)
                {
                    valueState = true;
                }
                 
                this->GridViewElement->EditorManager->EndEdit();
                this->TableElement->BeginUpdate();
                 
                for(auto i = 0; i < this->ViewInfo->Rows->Count; i++)
                {
                    this->ViewInfo->Rows[i]->Cells[this->ColumnIndex]->Value = valueState;
                }
     
                this->TableElement->EndUpdate(false);
     
                this->TableElement->Update(GridUINotifyAction::DataChanged);
            }
        }
     
        public: void SetCheckBoxState(Telerik::WinControls::Enumerations::ToggleState state)
        {
            this->_suspendProcessingToggleStateChanged = true;
            this->_checkbox->ToggleState = state;
            this->_suspendProcessingToggleStateChanged = false;
        }
     
        public: virtual void Attach(GridViewColumn^ data, System::Object^ context) override
        {
            this->GridHeaderCellElement::Attach(data, context);
            this->GridControl->ValueChanged += gcnew System::EventHandler(this, &CheckBoxHeaderCell::GridControl_ValueChanged);
        }
     
        public: virtual void Detach() override
        {
            if(this->GridControl != nullptr)
            {
                this->GridControl->ValueChanged -= gcnew System::EventHandler(this, &CheckBoxHeaderCell::GridControl_ValueChanged);
            }
     
            this->GridHeaderCellElement::Detach();
        }
     
        void GridControl_ValueChanged(System::Object^ sender, EventArgs^ e)
        {
            RadCheckBoxEditor^ editor = dynamic_cast<RadCheckBoxEditor^>(sender);
             
            if(editor != nullptr)
            {
                this->GridViewElement->EditorManager->EndEdit();
     
                if(static_cast<ToggleState>(editor->Value) == ToggleState::Off)
                {
                    SetCheckBoxState(ToggleState::Off);
                }
                else if(static_cast<ToggleState>(editor->Value) == ToggleState::On)
                {
                    bool found = false;
                    for each(GridViewRowInfo^ row in this->ViewInfo->Rows)
                    {
                        if (row != this->RowInfo && row->Cells[this->ColumnIndex]->Value == nullptr || !(bool)row->Cells[this->ColumnIndex]->Value)
                        {
                            found = true;
                            break;
                        }
                    }
     
                    if(found == false)
                    {
                        SetCheckBoxState(ToggleState::On);
                    }
                }
            }
        }
     
        private: RadCheckBoxElement^ _checkbox;
        private: bool _suspendProcessingToggleStateChanged;
        private: System::ComponentModel::Container ^components;
     
    #pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
        }
    #pragma endregion
    };
     
    public ref class CustomCheckBoxColumn : public GridViewCheckBoxColumn
    {
        public: virtual System::Type^ GetCellType(GridViewRowInfo^ row) override
        {
            if(dynamic_cast<GridViewTableHeaderRowInfo^>(row) != nullptr)
            {
                return CheckBoxHeaderCell::typeid;
            }
     
            return this->GridViewCheckBoxColumn::GetCellType(row);
        }
    };
  4. Stefan
    Admin
    Stefan avatar
    2908 posts

    Posted 17 Feb 2012 Link to this post

    Thank you for sharing your code with the community. If you have any other questions, do not hesitate to contact us.

    All the best,
    Stefan
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top