如何在代码隐藏的 Telerik RadGrid 的 TemplateColumn 中填充标签

How fill label inside TemplateColumn of Telerik RadGrid in code behind

这是我的 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 &amp; 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>

现在在后面的代码中,我想像这样在 TemplateColumns 中填充这些标签:

    DataTable table = new DataTable();
    table.Columns.Add("Type", typeof(string));
    table.Columns.Add("Date &amp; 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();

这是错误的输出:

我如何访问这些标签并填写它们?

如果不需要自定义标签,可以将GridTemplateColumn改为GridBoundColumn。 第一列作为示例:

  <telerik:GridBoundColumn DataField="Type" HeaderText="Type" UniqueName="TemplateColumn_Type"  ItemStyle-Width="100px" >
    <HeaderStyle Font-Bold="True" Font-Size="Medium" Width="100px" HorizontalAlign="Center" VerticalAlign="Middle" />
    </telerik:GridBoundColumn>

您只需要在 radgrid 列的 DataField 和 Datatable 中的列名中具有相同的值。 "Type" 在这种情况下。

如果您想使用 GridTemplateColumn,请更改文本值并像这样使用 DataBinder:

<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='<%#DataBinder.Eval(Container.DataItem, "Type")%>' ></asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>

应用于其余列。