How fill label inside TemplateColumn of Telerik RadGrid in code behind

3 posts, 1 answers
  1. Zakhaliya Ivarnovich
    Zakhaliya Ivarnovich avatar
    7 posts
    Member since:
    Feb 2010

    Posted 08 Nov 2019 Link to this post

    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. Answer
    Peter Milchev
    Admin
    Peter Milchev avatar
    804 posts

    Posted 13 Nov 2019 Link to this post

    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.
  3. Zakhaliya Ivarnovich
    Zakhaliya Ivarnovich avatar
    7 posts
    Member since:
    Feb 2010

    Posted 16 Nov 2019 in reply to Peter Milchev Link to this post

    Thanks for the help
Back to Top