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

RadGrid With Html Table <Table> in GridTemplateColumn

1 Answer 266 Views
Grid
This is a migrated thread and some comments may be shown as answers.
saba
Top achievements
Rank 1
saba asked on 03 Nov 2010, 06:24 AM
Hi !
I have a radgrid with a datasource creating dynamicaly.it means columns create programatically and add to radgrid,so before runtime it is not clear  number of columns or their names.
and some columns have header .it means a radgrid with 2 rows in header.
now i want to create a <table> with two <tr> and add columns header to <td>.
then add this <table> to GridTemplateColumn -> HeaderTemplate.
like this:
<telerik:GridTemplateColumn UniqueName="TemplateColumn" SortExpression="CompanyName"
                        InitializeTemplatesFirst="false">
                        <FooterTemplate>
                            Template column footer</FooterTemplate>
                        <FooterStyle VerticalAlign="Middle" HorizontalAlign="Center" />
                        <HeaderTemplate>
                            <table id="Table1" cellspacing="0" style="width:240px;" class="myTable">
                                <tr>
                                    <td colspan="2" align="center">
                                        <b>Contact details</b></td>
                                </tr>
                                <tr>
                                    <td style="width: 50%">
<asp:LinkButton CssClass="Button" ID="btnContName" Text="Contact name" ToolTip="Sort by ContactName"
CommandName='Sort' CommandArgument='ContactName' runat="server" /></td>

                                    <td style="width: 50%">
<asp:LinkButton CssClass="Button" ID="btnContTitle" Text="Contact title" ToolTip="Sort by ContactTitle"
CommandName='Sort' CommandArgument='ContactTitle' runat="server" /></td>

                                </tr>
                            </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table cellspacing="0" width="100%" class="myTable">
                                <tr>
                                    <td style="width: 50%">
                                        <%# Eval("ContactName") %>
                                    </td>
                                    <td style="width: 50%">
                                        <%# Eval("ContactTitle") %>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Center" />
                    </telerik:GridTemplateColumn>


but this is in design time.i want create this in run time.(code behind)
how can i do this?

thanks.

1 Answer, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 03 Nov 2010, 03:49 PM
Hi saba,

Take a look at this help topic. You can make a class that implements the ITemplate interface. And use instance of this class for the header template of your column. Please see the code snippet below:

private class MyHeaderTemplate : ITemplate
{
    public void InstantiateIn(Control container)
    {
        // TODO: Implement this method
        throw new NotImplementedException();
    }
}
 
private class MyItemTemplate : ITemplate
{
    public void InstantiateIn(Control container)
    {
        // TODO: Implement this method
        throw new NotImplementedException();
    }
}
 
protected void Page_Init(object sender, EventArgs e)
{
    RadGrid grid = new RadGrid();
    grid.AutoGenerateColumns = false;
    grid.DataSourceID = "MyDataSource";
    string templateColumnName = "My Column";
    GridTemplateColumn templateColumn = new GridTemplateColumn();
    templateColumn.ItemTemplate = new MyItemTemplate();
    templateColumn.HeaderTemplate = new MyHeaderTemplate();
    grid.MasterTableView.Columns.Add(templateColumn);
    form1.Controls.Add(grid);
}

Regards,
Vasil
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
Grid
Asked by
saba
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Share this question
or