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

  • Question

  • User296452190 posted

    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,  Im using a session state to pass the values of the row through a class, so when I get to the second webform I cant find a way to save show the information in an independent way.

    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, this is the code of my button that stores the value of the row and sends it through the session

         protected void Button2_Click(object sender, EventArgs e)
             {
                 List<Expedientes> listexp = new List<Expedientes>();
                 
                 foreach (GridViewRow row in GridView1.Rows)
                 {
    
                     Expedientes c = new Expedientes();
                     c.poliza = Convert.ToInt32(row.Cells[1].Text);
                     c.ramo = Convert.ToInt32(row.Cells[2].Text);
                     c.certif = Convert.ToInt32(row.Cells[3].Text);
                     c.nomrmo = row.Cells[4].Text;
                     c.compa = Convert.ToInt32(row.Cells[5].Text);
                     c.nomcia = row.Cells[6].Text;
                     c.rif = row.Cells[7].Text;
                     c.cct = Convert.ToInt32(row.Cells[8].Text);
                     listexp.Add(c);
                 }
                 if (listexp.Count > 0)
                 {
    
                     Session["Polizas"] = listexp;
                     Response.Redirect("Solicitado.aspx");
                 }

    The issue is to retrieve the value of the class from the second webform, because I want to handle each row with a separate variable.

    What do I need to put in the page load of webform2 apart from this

     protected void Page_Load(object sender, EventArgs e)
            {
    
                    if (Session["Application1_Data"] != null)
                    {
                        List<Expedientes> polizas = Session["Polizas"] as List<Expedientes>;
                        if (polizas != null )
                        {
                            if (polizas.Count > 1)
                            {
    
                            }
                            else
                            {
    
    
                            
                            }

    Regards, and thanks

    Friday, January 27, 2017 9:20 PM

Answers

  • User2103319870 posted

    You could loop through and read the values accordingly like below

    if (Session["Application1_Data"] != null)
                {
                    List<Expedientes> polizas = Session["Polizas"] as List<Expedientes>;
                    if (polizas != null)
                    {
                        if (polizas.Count > 1)
                        {
                            foreach (Expedientes exped in polizas)
                            {
                                //Assign value to controls here
                                TextBox1.Text = exped.poliza.ToString();
                                //Add other controls here
                            }
                        }
                        else
                        {
                            //If its single item then you can directly access the record
                            //Assign value to controls here
                            TextBox1.Text = exped[0].poliza.ToString();
                            //Add other controls here
                        }
                    }
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 27, 2017 9:58 PM

All replies

  • User2103319870 posted

    You could loop through and read the values accordingly like below

    if (Session["Application1_Data"] != null)
                {
                    List<Expedientes> polizas = Session["Polizas"] as List<Expedientes>;
                    if (polizas != null)
                    {
                        if (polizas.Count > 1)
                        {
                            foreach (Expedientes exped in polizas)
                            {
                                //Assign value to controls here
                                TextBox1.Text = exped.poliza.ToString();
                                //Add other controls here
                            }
                        }
                        else
                        {
                            //If its single item then you can directly access the record
                            //Assign value to controls here
                            TextBox1.Text = exped[0].poliza.ToString();
                            //Add other controls here
                        }
                    }
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 27, 2017 9:58 PM
  • User296452190 posted

    Wow, thanks!

    I can access the information with the foreach statement! I just have one issue remaining and it is that I get only 1 row of all the grid view selected, if more than 1 row is selected a don't really know how to display the diferent rows because they could be 5 or 6 or more

     foreach (Expedientes exped in polizas)
                                {
                                    //Assign value to controls here
                                    Label1.Text = exped.poliza.ToString();
                                    Label2.Text = exped.ramo.ToString();
                                    Label3.Text = exped.nomrmo.ToString();
                                    Label4.Text = exped.nomcia.ToString();
                                    Label5.Text = exped.rif.ToString();
                                    Label6.Text = exped.cct.ToString();
                                    Label7.Text = exped.certif.ToString();
                                    Label8.Text = exped.compa.ToString();
                                    //Add other controls here
                                }

    Monday, January 30, 2017 1:17 PM
  • User2103319870 posted

    it is that I get only 1 row of all the grid view selected, if more than 1 row is selected a don't really know how to display the diferent rows because they could be 5 or 6 or more

    You are seeiing one row because the values keep getting overwritten on every loop and you will see only the last values in textbox controls. If you have multiple values the best option is to use a gridview with Textbox on EditItemTemplate and assign the value to textbox.

    You also need to set the gridview to edit mode be default. Refer this link for doing that : http://highoncoding.com/Articles/219_GridView_All_Rows_in_Edit_Mode.aspx

    Monday, January 30, 2017 5:13 PM
  • User1169094377 posted

    You can also add textbox or label controls dynamically, but best option is to you data binding control like repeater which does give custom html layout.

    Monday, January 30, 2017 5:24 PM