Anyways i have found a solution for you
Page
<form id="form1" runat = "server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "False"
Font-Names = "Arial">
<Columns>
<asp:TemplateField HeaderText = "Item">
<ItemTemplate>
<asp:Label ID="lblItem" runat="server" Text='<%#Eval("Item") %>'></asp:Label>
<asp:TextBox ID="txtItem" runat="server" Text='<%#Eval("Item") %>' Visible = "false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Price") %>'></asp:Label>
<asp:TextBox ID="txtPrice" runat="server" Text='<%#Eval("Price") %>' Visible = "false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnEdit" runat="server" Text="Edit" OnClick = "Edit" />
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick = "Update" />
</form>
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Item"), new DataColumn("Price") });
dt.Rows.Add("Shirt", 200);
dt.Rows.Add("Football", 30);
dt.Rows.Add("Bat", 22.5);
dt.Rows.Add("Ring", 25);
dt.Rows.Add("Band", 77);
dt.Rows.Add("Glass", 57);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Edit(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
row.FindControl("txtItem").Visible = true;
row.FindControl("lblItem").Visible = false;
row.FindControl("txtPrice").Visible = true;
row.FindControl("lblPrice").Visible = false;
}
}
}
protected void Update(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label lblItem = (row.FindControl("lblItem") as Label);
Label lblPrice = (row.FindControl("lblPrice") as Label);
TextBox txtItem = (row.FindControl("txtItem") as TextBox);
TextBox txtPrice = (row.FindControl("txtPrice") as TextBox);
lblItem.Visible = true;
txtItem.Visible = false;
lblPrice.Visible = true;
txtPrice.Visible = false;
lblItem.Text = txtItem.Text;
lblPrice.Text = txtPrice.Text;
string item = lblItem.Text;
string price = lblPrice.Text;
//Update database row here
}
}
}
}
Output
Edit Mode