Display Sum (Total) of Checked GridView Rows on CheckBox check using jQuery in ASP.Net

yokeshmv10
 
on Aug 19, 2015 04:11 AM
9103 Views

I have a grid view with two columns A and B. Column A has Checkboxes and Column B has numeric values(int). When user checked checkboxes the checked checkboxes column B have to added and displayed in a Label. Anyone help me.. Thank u.. Have a nice day

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Shashikant
 
on Aug 19, 2015 05:28 AM

Here I have created sample that will help you out.

HTML

<div>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('[id*=chk]').on('change', function () {
                var value = 0;
                $('[id*=chk]:checked').each(function () {
                    var row = $(this).closest('tr');
                    value = value + parseInt(row.find('[id*=lblValue]').html());
                });
                $('[id*=lblTotal]').html(value);
            });
        });
    </script>
    <asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField HeaderText="Select">
                <ItemTemplate>
                    <asp:CheckBox ID="chk" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Values">
                <ItemTemplate>
                    <asp:Label ID="lblValue" runat="server" Text='<%# Eval("Value") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:Label ID="lblTotal" runat="server" />
</div>

Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Value", typeof(int)) });
        dt.Rows.Add(10);
        dt.Rows.Add(20);
        dt.Rows.Add(30);
        dt.Rows.Add(40);
        gvData.DataSource = dt;
        gvData.DataBind();
    }
}

Screenshot