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

Creating a custom Group Template

1 Answer 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joseph
Top achievements
Rank 1
Joseph asked on 06 Sep 2011, 09:25 AM
Hi guys,

I have a Gridview wich is grouped by a column.

Design
<GroupByExpressions>
                    <telerik:GridGroupByExpression>
                     
                        <SelectFields>
                            <telerik:GridGroupByField   FieldAlias="TL" FieldName="TargetLanguageCode" FormatString="{0:D}"
                                HeaderValueSeparator=": " HeaderText="">
                                 
                                </telerik:GridGroupByField>
                                 <telerik:GridGroupByField   FieldAlias="Flag" FieldName="FlagIcon" FormatString="{0:D}"
                                HeaderValueSeparator=": " HeaderText="">
                                 
                                </telerik:GridGroupByField>
                                 
                        </SelectFields>
                        <GroupByFields>
                            <telerik:GridGroupByField FieldName="TargetLanguageID" SortOrder="Descending"></telerik:GridGroupByField>
                             <telerik:GridGroupByField FieldName="FlagIcon" ></telerik:GridGroupByField>
                        </GroupByFields>
                    </telerik:GridGroupByExpression>
                </GroupByExpressions>

On the Grid ItemDataBound event I delete all the controls from the Group template and add a label and image
protected void gridGeneratePackages_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridGroupHeaderItem)
    {
        GridGroupHeaderItem item = e.Item as GridGroupHeaderItem;
        Image img = new Image();
        img.ImageUrl = ((DataRowView)e.Item.DataItem)[1].ToString();
        img.Width = 16;
        img.Height = 12;
 
        Label lbl = new Label();
        lbl.Text = ((DataRowView)e.Item.DataItem)[0].ToString(); ;
        lbl.Style.Add("margin-left", "3px");
        item.DataCell.Controls.Clear();
        item.DataCell.Controls.Add(img);
        item.DataCell.Controls.Add(lbl);
    }
}

This works perfectly when loaded, but when I click on a Grid Arrow to expand a Parent/Child, my added controls are removed and the ones in the design appears.

Any idea if there is a better solution ?

Thanks.





1 Answer, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 08 Sep 2011, 03:27 PM
Hello Joseph,

In order to implement the desired functionality, I recommend that you use the ItemCreated event for modifying the controls and the ItemDataBound event for binding them to data.
Please examine the Distinguishing the major differences between ItemCreated and ItemDataBound events help topic for additional information.

I hope this helps.

Regards,
Mira
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
Grid
Asked by
Joseph
Top achievements
Rank 1
Answers by
Mira
Telerik team
Share this question
or