jeudi 26 janvier 2017

Select complete rows from a gridview using a checkbox and transfer to another webform

I need help to select information from a gridview not to delete, I´ve seen multiple ways to delete and I've tried to adapt it to my code but I'm having some issues with it, This is my gridview code

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ItemStyle-Width="150px" ID="cbSelect" runat="server" AutoPostBack="True" OnCheckedChanged="cbSelect_CheckedChanged" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ItemStyle-Width="150px" ID="cbSelected" runat="server" AutoPostBack="True" OnCheckedChanged="cbSelected_CheckedChanged" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Poliza">
                <ItemTemplate>
                    <asp:Label ID="Poliza" runat="server" Text='<%# Bind("POLIZA") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField ItemStyle-Width="150px" DataField="RAMO"
                HeaderText="Ramo" />
            <asp:BoundField ItemStyle-Width="150px" DataField="CERTIF"
                HeaderText="Certificado" />
            <asp:BoundField ItemStyle-Width="150px" DataField="NOMRMO"
                HeaderText="Ramo" />
            <asp:BoundField ItemStyle-Width="150px" DataField="CIA"
                HeaderText="N. Comp" />
            <asp:BoundField ItemStyle-Width="150px" DataField="NOMCIA"
                HeaderText="Compañia" />
            <asp:BoundField ItemStyle-Width="150px" DataField="RIF"
                HeaderText="Ident" />
            <asp:BoundField ItemStyle-Width="150px" DataField="CCT"
                HeaderText="Num" />
        </Columns>
    </asp:GridView>

In my backend I have the code that fills the gridview which is working perfectly, also the code that evaluates if the checkbox has been checked.

Now, this is the code of my button that needs to pick the value of all the rows and transfer to another webform, normally I have done it with a "buttonfield" with "OnSelectedIndexChanged" but I can I do it with a checkbox?

protected void Button2_Click(object sender, EventArgs e)
 {
     List<string> test = new List<string>();

     foreach (GridViewRow gridViewRow in GridView2.Rows)
     {
         if (((CheckBox)gridViewRow.FindControl("cbSelected")).Checked)
         {
             string ejecutivoId = ((Label)gridViewRow.FindControl("Poliza")).Text;
             test.Add(ejecutivoId);
         }
     }
     if (test.Count > 0)
     {}




Aucun commentaire:

Enregistrer un commentaire