Click here to Skip to main content
16,020,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

VB
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()
Posted
Comments
[no name] 4-May-14 7:58am    
Okay so what is the problem then? If you want to "calculate" something, you need to perform some sort of mathematical operation and you are not.

1 solution

Study this example and adapt it to your need:
the aspx page:
XML
<%@ 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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900