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

RadButton with Event in GroupHeaderCell

2 Answers 105 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ian
Top achievements
Rank 1
Ian asked on 14 Oct 2011, 09:41 AM
Hi Telerik,

This isn't a question (unless you see ways to improve the class). Just giving a little back for all the help you guys have passed my way in the last year.

The class below allows a button to be placed in the Group Header cells of a grouped GridView and be able to subscribe to the button's clickevent. The event args pass back the GridViewGroupRowInfo. I'm using it to be able to identify and manipulate only the data rows associated with the particular group in question.

Hope some finds it useful
Regards
Ian

 
{
    public delegate void GroupHeaderCellButtonClickEventHandler(object sender, GroupHeaderButtonClickEventArgs e);
  
    public partial class CustomGridView : RadGridView
    {
        public event GroupHeaderCellButtonClickEventHandler GroupHeaderCellButtonClick;
  
        public CustomGridView()
        {
            InitializeComponent();
            base.CreateCell += new GridViewCreateCellEventHandler(CustomGridView_CreateCell);
        }
  
        void CustomGridView_CreateCell(object sender, GridViewCreateCellEventArgs e)
        {
  
            if (e.CellType == typeof(GridGroupContentCellElement) && e.Row.GetType() == typeof(GridGroupHeaderRowElement))
            {
                e.CellElement = new RadCustomGroupContentCell(e.Column, e.Row);
                RadCustomGroupContentCell cellElement = (RadCustomGroupContentCell)e.CellElement;
                cellElement.InternalGroupHeaderCellButtonClick+=new GroupHeaderCellButtonClickEventHandler(cellElement_InternalGroupHeaderCellButtonClick);
            }
        }
  
  
        void  cellElement_InternalGroupHeaderCellButtonClick(object sender, GroupHeaderButtonClickEventArgs e)
        {
            if (GroupHeaderCellButtonClick != null)
            {
                GroupHeaderCellButtonClick(sender, e);
            }
        }
  
        public override string ThemeClassName
        {
            get
            {
                return typeof(RadGridView).FullName;
            }
            set
            { }
        }
  
        protected internal class RadCustomGroupContentCell : GridGroupContentCellElement
        {
            public event GroupHeaderCellButtonClickEventHandler InternalGroupHeaderCellButtonClick;
            RadButtonElement theButton;
  
            public RadCustomGroupContentCell(GridViewColumn column, GridRowElement row)
                : base(column, row)
            {
  
            }
  
            protected override void DisposeManagedResources()
            {
                theButton.Click -= new EventHandler(theButton_Click);
                base.DisposeManagedResources();
            }
  
            void theButton_Click(object sender, EventArgs e)
            {
                InternalGroupHeaderCellButtonClick(sender, new GroupHeaderButtonClickEventArgs((GridViewGroupRowInfo)this.RowInfo));
            }
  
            protected override void CreateChildElements()
            {
                base.CreateChildElements();
  
                theButton = new RadButtonElement();
                theButton.Click += new EventHandler(theButton_Click);
                theButton.Text = "Insert";
                theButton.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
                theButton.Image = Resources.InsertRow16;
                this.Children.Add(theButton);
            }
  
            protected override SizeF ArrangeOverride(SizeF finalSize)
            {
                SizeF size = base.ArrangeOverride(finalSize);
  
                float buttonX =2;
                float buttonY = 4;
  
                RectangleF buttonRectangle = new RectangleF(buttonX, buttonY, theButton.DesiredSize.Width + 4, theButton.DesiredSize.Height + 2);
                theButton.Arrange(buttonRectangle);
  
                float buttonAndTextX = theButton.DesiredSize.Width + 4;
                RectangleF clientRect = new RectangleF(buttonAndTextX + buttonX, 0, finalSize.Width - buttonAndTextX - buttonX, finalSize.Height + 4);
                this.layoutManagerPart.Arrange(clientRect);
  
                return size;
            }
  
            protected override Type ThemeEffectiveType
            {
                get
                {
                    return typeof(GridGroupContentCellElement);
                }
                set
                { }
            }
        }
    }
}


The Event args look like this although more could be added as needed I imagine

public class GroupHeaderButtonClickEventArgs:EventArgs
{
    public GridViewGroupRowInfo GroupRowInfo
    { get; set; }
    public GroupHeaderButtonClickEventArgs(GridViewGroupRowInfo groupRowInfo)
    {
        this.GroupRowInfo = groupRowInfo;
    }
}

2 Answers, 1 is accepted

Sort by
0
Ian
Top achievements
Rank 1
answered on 14 Oct 2011, 09:51 AM
Need to take out the set{ } in the ThemeEffectiveType in RadCustomGroupContentCell class
0
Svett
Telerik team
answered on 18 Oct 2011, 02:58 PM
Hi Ian,

Thank you for sharing your code with the community. I confirm that you are following all good practices advocated by Telerik.

Regards,
Svett
the Telerik team

Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

Tags
GridView
Asked by
Ian
Top achievements
Rank 1
Answers by
Ian
Top achievements
Rank 1
Svett
Telerik team
Share this question
or