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

RadComboBox in the GridTableHeaderCell of a RadGrid

1 Answer 74 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Doug
Top achievements
Rank 1
Doug asked on 04 Aug 2017, 05:23 PM

Greetings, we're dynamically generating the columns and GridColumnGroups of a RadGrid in codebehind. At the point where we're adding a new column group to the MasterTableView of the RadGrid, we're establishing a specified GridColumnGroup HeaderText and Name (as a token). Then in the RadGrid ItemCreated event handler, we're detecting that the GridHeaderItem is being created...and then iterating over the TableCells in the GridHeaderItem to find the cell that starts with that token. . .and finally inserting a RadComboBox in the controls collection of that TableCell.

This worked flawlessly until the last Telerik update. Now all that is seen is the GridHeaderItem HeaderText/Name, instead of the RadComboBox. Any assistance would be greatly appreciated. Thanks!

1 Answer, 1 is accepted

Sort by
0
Peter Milchev
Telerik team
answered on 08 Aug 2017, 01:19 PM
Hello Doug,

We have investigated the scenario and it turns out the Grid is clearing the controls collection at a later stage. 

That is why the control should be created early in the page lifecycle but should be added to the controls collection at a later stage, the PreRender event for example.

Please find attached a sample project implementing the suggested approach.

protected void RadGrid1_OnItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridHeaderItem)
    {
        var gridHeaderItem = (e.Item as GridHeaderItem);
        var cells = gridHeaderItem.Cells;
        foreach (TableCell cell in cells)
        {
            if (cell.Text.StartsWith("Middle") && cell.Controls.Count == 0)
            {
                cell.Controls.Clear();
                cell.Text = string.Empty;
                cell.BackColor = Color.LightGreen;
 
                var box = new RadComboBox { AutoPostBack = true, Visible = true };
                box.Items.Add(new RadComboBoxItem("a", "b"));
                box.SelectedValue = "b";
                box.ID = Guid.NewGuid().ToString();
                e.Item.PreRender += (s, a) =>
                {
                    cell.Controls.Add(box);
                };
            }
        }
    }
}

More on anonymous functions and lambda expressions in C# could be found here: Lambda Expressions (C# Programming Guide).

Regards,
Peter Milchev
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Doug
Top achievements
Rank 1
Answers by
Peter Milchev
Telerik team
Share this question
or