Count selected CheckBox rows in a GridView

1

How do I select a table ( GridView ), write and in the other GridView show the amount of CheckBox chosen from the previous table in the current table?

    
asked by anonymous 02.07.2014 / 15:54

2 answers

1

Nix,

Take a look at these two links that talk about checkbox in GridView:

link

link

    
02.07.2014 / 16:13
0

Example:

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridDados" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Status") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Id" HeaderText="Id" />
                    <asp:BoundField DataField="Nome" HeaderText="Nome" />
                </Columns>
            </asp:GridView>
        </div>
        <div>
            <asp:Button ID="BtnEnviar" runat="server" Text="Enviar" OnClick="BtnEnviar_Click" /></div>
        <div>
            <asp:GridView ID="GridRecebe" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Status") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Id" HeaderText="Id" />
                    <asp:BoundField DataField="Nome" HeaderText="Nome" />
                </Columns>
            </asp:GridView>
        </div>
        <div>
           <asp:Label Text="0" ID="LblQuantidade" runat="server" />
        </div>
    </form>
</body>
</html>

.cs code

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Carregar_Dados();
            }
        }
        public void Carregar_Dados()
        {
            GridDados.DataSource = new object[]{
                new { Status=false, Id = 1, Nome = "Nome 1" },
                new { Status=false, Id = 2, Nome = "Nome 2" },
                new { Status=false, Id = 3, Nome = "Nome 3" }
            }.ToArray();
            GridDados.DataBind();
        }

        protected void BtnEnviar_Click(object sender, EventArgs e)
        {

            int i = -1;                        
            object[] obj = new object[GridDados.Rows.Count];                        
            foreach (GridViewRow row in GridDados.Rows)
            {                
                CheckBox chk = (CheckBox)row.Cells[0].FindControl("CheckBox1");
                if (chk.Checked)
                {
                    i++;
                    obj.SetValue(new { Status = chk.Checked, Id = int.Parse(row.Cells[1].Text), Nome = row.Cells[2].Text }, i);
                }
            }
            GridRecebe.DataSource = obj.Where(x => x != null).ToList();
            GridRecebe.DataBind();
            LblQuantidade.Text = (i + 1).ToString();
        }
    }
}

Example Screen:

    
02.07.2014 / 17:08