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

How fill label inside TemplateColumn of Telerik RadGrid in code behind

2 Answers 882 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Zakhaliya Ivarnovich
Top achievements
Rank 1
Zakhaliya Ivarnovich asked on 08 Nov 2019, 11:42 PM
Here is my RadGrid aspx :   

     
<telerik:RadGrid ID="grd_transactions" runat="server" GroupPanelPosition="Top" AutoGenerateColumns="False">
            <GroupingSettings CollapseAllTooltip="Collapse all groups"></GroupingSettings>
            <MasterTableView>
                <Columns>
                    <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn_Type column" HeaderText="Type" UniqueName="TemplateColumn_Type">
                        <HeaderStyle Font-Bold="True" Font-Size="Medium" Width="100px" HorizontalAlign="Center" VerticalAlign="Middle" />
                        <ItemTemplate>
                            <asp:Label ID="lbl_Type" runat="server" Text="lbl_Type"></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn_Date_Time_UTC_GMT column" HeaderText="Date & Time (UTC/GMT)" UniqueName="TemplateColumn_Date_Time_UTC_GMT">
                        <HeaderStyle Font-Bold="True" Font-Size="Medium" Width="200px" HorizontalAlign="Center" VerticalAlign="Middle" />
                        <ItemTemplate>
                            <asp:Label ID="lbl_Date_Time_UTC_GMT" runat="server" Text="lbl_Date_Time_UTC_GMT"></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn_BTC_Investment_Address column" HeaderText="BTC Investment Address" UniqueName="TemplateColumn_BTC_Investment_Address">
                        <HeaderStyle Font-Bold="True" Font-Size="Medium" Width="600px" HorizontalAlign="Center" VerticalAlign="Middle" />
                        <ItemTemplate>
                            <asp:Label ID="lbl_BTC_Investment_Address" runat="server" Text="lbl_BTC_Investment_Address"></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn_Deposit_Amount column" HeaderText="Deposit Amount" UniqueName="TemplateColumn_Deposit_Amount">
                        <HeaderStyle Font-Bold="True" Font-Size="Medium" Width="150px" HorizontalAlign="Center" VerticalAlign="Middle" />
                        <ItemTemplate>
                            <asp:Label ID="lbl_Deposit_Amount" runat="server" Text="lbl_Deposit_Amount"></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn_Payout_Amount column" HeaderText="Payout Amount" UniqueName="TemplateColumn_Payout_Amount">
                        <HeaderStyle Font-Bold="True" Font-Size="Medium" Width="150px" HorizontalAlign="Center" VerticalAlign="Middle" />
                        <ItemTemplate>
                            <asp:Label ID="lbl_Payout_Amount" runat="server" Text="lbl_Payout_Amount"></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn_Status column" HeaderText="Status" UniqueName="TemplateColumn_Status">
                        <HeaderStyle Font-Bold="True" Font-Size="Medium" Width="150px" HorizontalAlign="Center" VerticalAlign="Middle" />
                        <ItemTemplate>
                            <asp:Label ID="lbl_Status" runat="server" Text="lbl_Status"></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>


Now in code behind i want to fill those labels inside TemplateColumns like this :  
 

DataTable table = new DataTable();
table.Columns.Add("Type", typeof(string));
table.Columns.Add("Date & Time (UTC/GMT)", typeof(string));
table.Columns.Add("BTC Investment Address", typeof(string));
table.Columns.Add("Deposit Amount", typeof(string));
table.Columns.Add("Payout Amount", typeof(string));
table.Columns.Add("Status", typeof(string));
table.Rows.Add("1", "2", "3", "4" ,"5", "6");
table.Rows.Add("1", "2", "3", "4", "5", "6");
table.Rows.Add("1", "2", "3", "4", "5", "6");
table.Rows.Add("1", "2", "3", "4", "5", "6");
table.Rows.Add("1", "2", "3", "4", "5", "6");
grd_transactions.DataSource = table;
grd_transactions.DataBind();


Here is the wrong output.

How can i access those labels & fill them?

2 Answers, 1 is accepted

Sort by
0
Accepted
Peter Milchev
Telerik team
answered on 13 Nov 2019, 12:38 PM

Hello Zakhaliya,

The text for the labels is set statically and the and if you need them to be bound to the data source, you would need to use the Eval or Bind expressions instead: 

<ItemTemplate>
    <asp:Label ID="lbl_Type" runat="server" Text='<%# Eval("ShipCountry") %>'></asp:Label>
</ItemTemplate>

Or

<ItemTemplate>
    <asp:Label ID="lbl_Type" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ShipCountry") %>'></asp:Label>
</ItemTemplate>

Regarding the data binding, we strongly recommend using the AdvancedDataBinding, i.e. using the NeedDataSource event instead of the .DataBind() method:

Regards,
Peter Milchev
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Zakhaliya Ivarnovich
Top achievements
Rank 1
answered on 16 Nov 2019, 05:03 PM
Thanks for the help
Tags
Grid
Asked by
Zakhaliya Ivarnovich
Top achievements
Rank 1
Answers by
Peter Milchev
Telerik team
Zakhaliya Ivarnovich
Top achievements
Rank 1
Share this question
or