0

我是 Telerik 库的新手,我的首要任务之一是获取 Telerik RadGrid 的数字列中的值的总数。该列是一个 GridClientSelectColumn。我知道通过设置 Aggregate="Sum",页脚将读取该列的总和。问题是我只想要所选行的总和。有人可以提供一个演示。先感谢您!

4

1 回答 1

1

请检查下面的代码片段。

注意:如果您想通过标签代替 datakey 执行以下操作,请查看链接。

JS代码

 <telerik:RadCodeBlock ID="RadCodeBlock" runat="server">
    <script type="text/javascript">
        function RowSelected(sender, args) {

            var grid = $find("<%=RadGrid1.ClientID %>");

            var totalAmount = 0;

            if (grid) {
                var MasterTable = grid.get_masterTableView();
                var Rows = MasterTable.get_dataItems();
                for (var i = 0; i < Rows.length; i++) {
                    var row = Rows[i];
                    if (row.get_selected() == true) {

                        totalAmount = totalAmount + parseFloat(row.getDataKeyValue("ID"));
                    }
                }
            }

            if ($("span[id$='myFooter']").length > 0) {
                $("span[id$='myFooter']").get(0).innerHTML = "sum is : " + totalAmount;
            }
        }
    </script>
</telerik:RadCodeBlock>

.aspx

  <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
        ShowFooter="true" AllowMultiRowSelection="true">
        <MasterTableView ClientDataKeyNames="ID">
            <Columns>
                <telerik:GridClientSelectColumn UniqueName="columnSelect">
                </telerik:GridClientSelectColumn>
                <telerik:GridTemplateColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                    <ItemTemplate>
                        <asp:Label ID="txtID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label ID="myFooter" runat="server" Text="sum is : 0"></asp:Label>
                    </FooterTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
        <ClientSettings>
            <Selecting AllowRowSelect="true" />
            <ClientEvents OnRowSelected="RowSelected" OnRowDeselected="RowSelected" />
        </ClientSettings>
    </telerik:RadGrid>

.aspx.cs

 protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DateTime dt = DateTime.Now;

    dynamic data = new[] {
            new { ID = 1, Name ="Name_1", customdate=dt,NO = 11.00},
            new { ID = 2, Name = "Name2", customdate=dt,NO = 11.00},
            new { ID = 3, Name = "Name3", customdate=dt,NO = 11.00},
            new { ID = 4, Name = "Name4", customdate=dt,NO = 11.44},
            new { ID = 5, Name = "Name5", customdate=dt,NO = 10.4}
        };

    RadGrid1.DataSource = data;
}
于 2012-05-24T09:40:54.420 回答