Click here to Skip to main content
15,923,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,
I have an grid view and update button is outside the grid view. There is a image button Edit inside grid view. When i am clicking that edit button text boxes appears outside the grid view where i have to edit. But my update button which is button 3 not updating the corresponding row. it updates the whole Grid view. how the update button only update that row which edit button i have clicked. This is my code .please help
C#
protected void Button3_Click(object sender, EventArgs e)
{
  foreach ( GridView row in GridView1.Rows)
  {
        Label ID = (Label)row.FindControl("ID");
        Label Vendor_Name = (Label)row.FindControl("Vendor_Name");
        Label Asset_Type = (Label)row.FindControl("Asset_Type");

        Label status = (Label)row.FindControl("Status");
        Label Email = (Label)row.FindControl("Email");
        Label Date_Of_Registration = (Label)row.FindControl("Date_Of_Registration");
        Label Phone_Number = (Label)row.FindControl("Phone_Number");
        Label Vendor_Address = (Label)row.FindControl("Vendor_Address");
        Label Vendor_Billing_Address = (Label)row.FindControl("Vendor_Billing_Address");
        //TextBox1.Text = Vendor_Name.Text;
        //TextBox7.Text = Asset_Type.Text;
        //Status.SelectedValue = Status.Text;
        //TextBox5.Text = Email.Text;
        //TextBox6.Text = Phone_Number.Text;
        //TextBox4.Text = Date_Of_Registration.Text;
        //TextBox8.Text = Vendor_Address.Text;
        //TextBox9.Text = Vendor_Billing_Address.Text;
        con.Open();
        string sql = "update VandorDetail set Vendor_Name='" +
    TextBox1.Text + "',Asset_Type='" + TextBox7.Text + "',Email='" +
   TextBox5.Text + "',Status='" + Status.SelectedValue + "',Date_Of_Registration='" + TextBox4.Text + "',Phone_Number='" + TextBox6.Text + "',Vendor_Address='" + TextBox8.Text + "',Vendor_Billing_Address='" + TextBox9.Text + "' where ID='" +
   ID.Text + "'";
        SqlCommand cmd = new SqlCommand(sql);
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        con.Close();
        Populategridview();
    }
}



[Edit member="Tadit"]
Added pre tags.
Added correct Tags.
[/Edit]
Posted
v3

1 solution

This is happening because you are updating all the rows...
C#
foreach ( GridView row in GridView1.Rows)

To update only the particular row edited, you have to store one reference or Key of that Row somewhere either on ViewState or Session or a HiddenField or something. You have one ID column. So, you can store that as a reference.

Then inside this Button 3 Click Event, read that Key and update that particular Row.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900