anjali khan

anjali khan

  • NA
  • 293
  • 66.6k

how to edit cell in a gv without using button in a asp.net?

Jul 28 2015 1:25 AM
how to edit cell in a gv without using button in a asp.net?
 
I m using this code
 

how to edit the perticular cell without using button...
if i click the the perticular cell so data should be updated ..

Design page--

<div style="text-align:center">
      <asp:gridview ID="gridview1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" Width="100%">
       <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="50%" />
         <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="50%" />
          <asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="50%" />
       </Columns>
      </asp:gridview>
    </div>

code---

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                DataTable dt = new DataTable();

                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("City") });

                dt.Rows.Add(1, "Anamika", "Bangalore");

                dt.Rows.Add(2, "Sunny", "Chennai");

                dt.Rows.Add(3, "Monika", "Bangalore");

                dt.Rows.Add(4, "Jyoti", "Chennai");

                gridview1.DataSource = dt;

                gridview1.DataBind();

            }
        }
       
        protected void gridview1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {                
                    TextBox txt = new TextBox();
                    txt.Text = e.Row.Cells[i].Text;                
                    e.Row.Cells[i].Text = "";
                    e.Row.Cells[i].Controls.Add(txt);
                }
            }
        }
    }