Count selected Checkbox lines in a Gridview

Asked

Viewed 1,095 times

1

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

2 answers

1

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>

Code . Cs

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

Screen Example:

inserir a descrição da imagem aqui

  • these examples are very good, but what I’m trying to do would be the following, in a gridview I would choose 4 lines with checkbox, and when I load the same grid view would have two behavior, one would load show the same record selected from the chekcbox, another in another, would show the amount of fields selected

  • You want to persist the information on the grid with check, the selected ones would be like you left earlier and a label with the selected quantity? I was in doubt...

  • @Nix I made the change, now you need to adapt to your model, because in your question you did not give me better subsidies to answer! if you can do the same I change again!

  • It would be as follows, in a gridview I have the fields, I select them and write them in the database, then in another gridview I would have the quandity of selected fields, for example if I selected 5 fields and recorded, while I pressed in another gridview, i would have the amount of fields selected with check, in our case 5, with this example quoted I also can exiblos within a gridview ??

  • Yes ... the example is a step you must take to get to what you want !!! is the same logic only that has the interaction in the bank... @Nix

Browser other questions tagged

You are not signed in. Login or sign up in order to post.