如何从GridView,asp.net计算所选行的总和 [英] How to Calculate Sum of selected rows from GridView, asp.net
本文介绍了如何从GridView,asp.net计算所选行的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的弟弟,
请帮助我只想计算lblAmount的代码。下面的Gridview中的字段是我的代码。我正在给用户选择多行
Dear Brother,
Please help for the code I only want to calculate "lblAmount" Field from Gridview below is my code. I am giving a user to select multiple Rows
Dim cnt As Integer
For cnt = 0 To Me.GridView10.Rows.Count - 1
Dim chkbox As System.Web.UI.WebControls.CheckBox = TryCast(Me.GridView10.Rows(cnt).FindControl("chksel"), System.Web.UI.WebControls.CheckBox)
Dim txtlblid As System.Web.UI.WebControls.Label = TryCast(GridView10.Rows(cnt).FindControl("lblID"), System.Web.UI.WebControls.Label)
Dim TotalAmount As System.Web.UI.WebControls.Label = TryCast(GridView10.Rows(cnt).FindControl("lblamount"), System.Web.UI.WebControls.Label)
If chkbox.Checked Then
lbltotalDB.Text = Convert.ToInt32(TotalAmount.Text)
End If
Next
GridView10.DataBind()
推荐答案
研究此示例并根据您的需要进行调整:
aspx页面:
Study this example and adapt it to your need:
the aspx page:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default7.aspx.vb" Inherits="Default7" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Label1" runat="server" Text="Checkbox"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
背后的代码:
the code behind:
Imports System.Data
Partial Class Default7
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.Add("Quantity")
dt.Rows.Add("1")
dt.Rows.Add("2")
dt.Rows.Add("3")
dt.Rows.Add("4")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sum As Integer = 0
For Each row As GridViewRow In GridView1.Rows
Dim chkbox As CheckBox
chkbox = CType(row.FindControl("CheckBox1"), CheckBox)
If chkbox.Checked Then
sum = sum + row.Cells(1).Text.ToString()
End If
Next
TextBox1.Text = sum
End Sub
End Class
这篇关于如何从GridView,asp.net计算所选行的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文