如何只为选定的行获取Telerik RadGrid GridClientSelectColumn的总和?

时间:2012-05-22 20:41:08

标签: asp.net asp.net-ajax telerik telerik-grid radgrid

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

1 个答案:

答案 0 :(得分:1)

请查看下面的代码段。

注意:如果您想在数据键的位置按标记进行操作,请检查this链接。

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;
}