Click here to Skip to main content
14,773,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am generating dynamic textboxes and dropdownlist by maintaining the viewstate and bind it in gridview.
When I delete any row from gridview using LinkButton Delete, instead of deleting that row, it deleted the row below it.Here is the code:
protected void LinkButton1_Click(object sender, EventArgs e)
  {

          LinkButton lb = (LinkButton)sender;
          GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
          int rowID = gvRow.RowIndex + 1;
          if (ViewState["CurrentTable"] != null)
          {
              DataTable dt = (DataTable)ViewState["CurrentTable"];
              if (dt.Rows.Count > 1)
              {
                  if (gvRow.RowIndex < dt.Rows.Count - 1)
                  {
                      //Remove the Selected Row data
                      dt.Rows.Remove(dt.Rows[rowID]);
                  }
              }
              //Store the current data in ViewState for future reference
              ViewState["CurrentTable"] = dt;
              //Re bind the GridView for the updated data
              Gridview1.DataSource = dt;
              Gridview1.DataBind();
          }


          SetPreviousData();
      }


private void SetPreviousData()
  {

          int rowIndex = 0;
          if (ViewState["CurrentTable"] != null)
          {
              DataTable dt = (DataTable)ViewState["CurrentTable"];

              if (dt.Rows.Count > 0)
              {
                  for (int i = 0; i < dt.Rows.Count; i++)
                  {

                      DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DROPDOWNLIST1");
                      box1.DataSource = getitemoutput.DtCategory;
                      box1.DataTextField = "item_name";
                      box1.DataValueField = "store_item_id";
                      box1.DataBind();
                      box1.Items.Insert(0, "Select store");
                      TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                      TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                      TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                      TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox5");

                      box1.Text = dt.Rows[i]["Column1"].ToString();
                      box2.Text = dt.Rows[i]["Column2"].ToString();
                      box3.Text = dt.Rows[i]["Column3"].ToString();
                      box4.Text = dt.Rows[i]["Column4"].ToString();
                      box5.Text = dt.Rows[i]["Column5"].ToString();

                      rowIndex++;
                  }
              }
          }

  }
Posted
Updated 3-Dec-13 21:59pm
v2

1 solution

Check this Article from where you tried to implement this code : Dynamically adding and deleting rows from ASP.NET GridView[^]

Verify why it doesn't work for you.
   
Comments
ssss0001 4-Dec-13 3:27am
   
Thanks

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