locked
Bulk Update without CheckBox RRS feed

  • Question

  • User-1499457942 posted

    Hi

     How to bulk Update records without using CheckBoxes. I want on click of button all records should get Updated

    Thanks

    Thursday, November 29, 2018 4:45 AM

All replies

  • User-893317190 posted

    Hi JagjitSingh,

    You could use textbox with label in gridview's template field and deal with updating in the button's event.

    Below is my code.

    <form id="form1" runat="server">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1"   >
                <Columns>
                    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
                    <asp:TemplateField HeaderText="name">
                        <ItemTemplate>
                            <%-- make label visible and textbox invisible when not in edit mode --%>
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("name") %>'></asp:Label>   
                            <asp:TextBox ID="TextBox1" runat="server" Visible="false" Text='<%#Eval("name") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField >
                      <asp:TemplateField HeaderText="price">
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%#Eval("price") %>'></asp:Label>
                            <asp:TextBox ID="TextBox2" runat="server" Visible="false" Text='<%#Eval("price") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                      <asp:TemplateField HeaderText="marca">
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%#Eval("marca") %>'></asp:Label>
                            <asp:TextBox ID="TextBox3" runat="server" Visible="false"  Text='<%#Eval("marca") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    
                </Columns>
            </asp:GridView>
            <asp:Button ID="Button1" runat="server" Text="EditAll" OnClick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" Text="CancelEidtAll" OnClick="Button2_Click" />
            <asp:Button ID="Button3" runat="server" Text="UpdataAll" OnClick="Button3_Click" />
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EntityExe %>" SelectCommand="SELECT * FROM [perfume]"></asp:SqlDataSource>
        </form>

    Code behind.

      protected void Button1_Click(object sender, EventArgs e)
            {
                foreach ( GridViewRow row in GridView1.Rows)
                {
                    // to enter edit mode make all the textbox show , all the label hide
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                     Label label=   row.FindControl("label1") as Label;
                        label.Visible = false;
                        TextBox box = row.FindControl("textbox1") as TextBox;
                        box.Visible = true;
    
                       label = row.FindControl("label2") as Label;
                        label.Visible = false;
                       box = row.FindControl("textbox2") as TextBox;
                        box.Visible = true;
    
    
                        label = row.FindControl("label3") as Label;
                        label.Visible = false;
                       box = row.FindControl("textbox3") as TextBox;
                        box.Visible = true;
    
                    }
                }
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                // to leave edit mode make all the textbox hide , all the label show
                foreach (GridViewRow row in GridView1.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        Label label = row.FindControl("label1") as Label;
                        label.Visible = true;
                        TextBox box = row.FindControl("textbox1") as TextBox;
                        box.Visible = false;
    
                        label = row.FindControl("label2") as Label;
                        label.Visible = true;
                        box = row.FindControl("textbox2") as TextBox;
                        box.Visible = false;
    
    
                        label = row.FindControl("label3") as Label;
                        label.Visible = true;
                        box = row.FindControl("textbox3") as TextBox;
                        box.Visible = false;
    
                    }
                }
            }
            private static string constr = ConfigurationManager.ConnectionStrings["EntityExe1"].ConnectionString;
            protected void Button3_Click(object sender, EventArgs e)
            {
              // on updating ,find all the textbox and update all the row in a for loop
                foreach (GridViewRow row in GridView1.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        Label label = row.FindControl("label1") as Label;
                        label.Visible = true;
                        TextBox box = row.FindControl("textbox1") as TextBox;
                        box.Visible = false;
                        string name = box.Text;
                       
                        label = row.FindControl("label2") as Label;
                        label.Visible = true;
                        box = row.FindControl("textbox2") as TextBox;
                        box.Visible = false;
                        int price = int.Parse(box.Text);
                      
    
                        label = row.FindControl("label3") as Label;
                        label.Visible = true;
                        box = row.FindControl("textbox3") as TextBox;
                        
                        box.Visible = false;
                        string marca = box.Text;
    
    
                        int id = int.Parse(row.Cells[0].Text);
    
    
                        using (SqlConnection con = new SqlConnection(constr))
                        {
                            using (SqlCommand com = new SqlCommand("update [perfume] set name=@name ,price=@price,marca=@marca where id=@id ", con))
                            {
                                com.Parameters.AddWithValue("name", name);
                                com.Parameters.AddWithValue("price", price);
                                com.Parameters.AddWithValue("marca", marca);
                                com.Parameters.AddWithValue("id", id);
                                con.Open();
                                 com.ExecuteNonQuery();
                            }
                        }
    
                    }
                    //at last, rebind the gridview to show the new data
                   GridView1.DataBind();
                }
            }

    The result.

    Best regards,

    Ackerly Xu

    Friday, November 30, 2018 2:53 AM