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

Extend RadGridView

1 Answer 209 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Naeem
Top achievements
Rank 1
Naeem asked on 02 Mar 2010, 03:49 PM
Hi,

I need to extend RadGridView. I want to add child item in GridHeaderElement. How can I achieve this?

Thanks

My code: It add combobox rad element but it does not place at proper location and drop down is not shown.
public partial class RadGridViewEx : RadGridView 
    { 
        public RadGridViewEx():base() 
        { 
            InitializeComponent(); 
        } 
 
        protected override void CreateChildItems(RadElement parent) 
        { 
            base.CreateChildItems(parent); 
 
            GridHeaderElement header = ((Telerik.WinControls.UI.GridTableElement)(this.GridElement)).HeaderElement; 
 
            RadComboBoxElement cb = new RadComboBoxElement(); 
            RadItem item = new RadItem(); 
            item.Text = "A"
            cb.Items.Add(item); 
            item = new RadItem(); 
            item.Text = "B"
            cb.Items.Add(item); 
            item = new RadItem(); 
            item.Text = "C"
            cb.Items.Add(item); 
            item = new RadItem(); 
            item.Text = "D"
            cb.Items.Add(item); 
 
            //cb.Location = new Point(0, 25); 
            cb.Size = new Size(100, 25); 
            cb.DropDownHeight = 300
            cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged); 
            header.GroupPanel.Children.Add(cb); 
           
 
        }             
 
        void cb_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            RadComboBoxElement cb = sender as RadComboBoxElement; 
             
            MessageBox.Show(((RadItem)cb.SelectedItem).Text); 
        } 
         
    }     

1 Answer, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 04 Mar 2010, 08:00 AM
Hello Naeem,

You cannot create RadComboBox in GridHeaderElement by overriding CreateChildItems method of RadGridView, because you cannot position it on this step of the life cycle of the control. You should extend and create your GridHeaderElement where you can create your custom element that contains the combo box element. When you do that you have to replace the default header instance with your implementation. Therefore, you should replace also the default TableViewDefinition, GridTableElement and GridHeaderElement with your implementation versions. I have implemented GridTopHeaderElement  which contains the combo box that you want to show on the top of the grid.

You may use the following code snippet:

public class TableViewDefinitionEx : TableViewDefinition
{
    public override GridViewUIElement CreateViewUIElement(GridViewInfo viewInfo)
    {
        GridViewUIElement view = new GridTableElementEx();
        view.Initialize(viewInfo);
 
        PropertyInfo property = typeof(GridViewInfo).GetProperty("GridViewElement");
        property.SetValue(viewInfo, view, null);
 
        return view;
    }
}

public class GridTableElementEx : GridTableElement
{
    protected override void CreateChildElements()
    {
        base.CreateChildElements();
 
        this.Children.Remove(this.HeaderElement);
 
        GridHeaderElement headerElement = new GridHeaderElementEx(this);
 
        FieldInfo fieldInfo = typeof(GridTableElement).GetField("headerElement", BindingFlags.NonPublic | BindingFlags.Instance);
 
        fieldInfo.SetValue(this, headerElement);
 
        this.Children.Add(this.HeaderElement);
    }
 
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(GridTableElement);
        }
    }
}

public class GridHeaderElementEx : GridHeaderElement
{
    private GridTopHeaderElement header;
 
    public GridHeaderElementEx(GridViewUIElement viewUIElement)
        : base(viewUIElement)
    {
    }
 
    protected override void CreateChildElements()
    {
        this.header = new GridTopHeaderElement();
        this.Children.Add(this.header);
 
        base.CreateChildElements();
    }
 
    protected override SizeF MeasureOverride(SizeF availableSize)
    {
        SizeF size = base.MeasureOverride(availableSize);
 
        this.header.Measure(availableSize);
        size += this.header.DesiredSize;
 
        return size;
    }
 
    protected override SizeF ArrangeOverride(SizeF finalSize)
    {
        RectangleF clientRect = this.GetClientRectangle(finalSize);
        float x = clientRect.X;
        float y = clientRect.Y;
 
        foreach (RadElement element in this.Children)
        {
            float height = element.DesiredSize.Height;
 
            if (y + height >= clientRect.Bottom)
            {
                height = clientRect.Bottom - y;
            }
 
            element.Arrange(new RectangleF(x, y, clientRect.Width, height));
            y += height;
        }
 
        return finalSize;
    }
}

public class GridTopHeaderElement : LightVisualElement
{
    private RadComboBoxElement comboBox;
 
    protected override void InitializeFields()
    {
        base.InitializeFields();
        this.UseNewLayoutSystem = true;
        this.Padding = new System.Windows.Forms.Padding(2);
    }
    protected override void CreateChildElements()
    {
        comboBox = new RadComboBoxElement();
        RadItem item = new RadComboBoxItem();
        item.Text = "A";
        comboBox.Items.Add(item);
        item = new RadComboBoxItem();
        item.Text = "B";
        comboBox.Items.Add(item);
        item = new RadComboBoxItem();
        item.Text = "C";
        comboBox.Items.Add(item);
        item = new RadComboBoxItem();
        item.Text = "D";
        comboBox.Items.Add(item);
 
        comboBox.AutoSize = false;
        comboBox.Size = new Size(100, 20);
        comboBox.DropDownHeight = 300;
        comboBox.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
 
        this.Children.Add(comboBox);
    }
 
    private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadComboBoxElement cb = sender as RadComboBoxElement;
        RadComboBoxItem selectedItem = cb.SelectedItem as RadComboBoxItem;
 
        if (selectedItem != null)
        {
            MessageBox.Show(selectedItem.Text);
        }
    }
 
    protected override SizeF ArrangeOverride(SizeF finalSize)
    {
        RectangleF clientSize = this.GetClientRectangle(finalSize);
 
        RectangleF newClientSize = new RectangleF(clientSize.X + clientSize.Width / 2 - comboBox.DesiredSize.Width / 2, clientSize.Y, clientSize.Size.Width, clientSize.Size.Height);
 
        comboBox.Arrange(newClientSize);
 
        return finalSize;
    }
 
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(GridGroupPanel);
        }
    }
}

Due to some limitation of the public API, I had to use reflection in some cases. To use the extended version of  TableViewDefinition you have to initialize the ViewDefinition property of the grid with an instance of
TableViewDefinitionEx.

this.radGridView.ViewDefinition = new TableViewDefinitionEx();

Let us know if you need further assistance.

All the best,
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
Naeem
Top achievements
Rank 1
Answers by
Svett
Telerik team
Share this question
or