Hi ms65672,
When you move to the next cell the previous cell value is saved in the textbox and the textbox is hiding and display the label. If you want to display the updated value in the label then you need to assign the updated value to label control.
Check this example. Now please take its reference and correct your code.
HTML
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' CssClass="hideControl"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblAddress" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
<asp:TextBox ID="txtAddress" runat="server" Text='<%# Eval("Address") %>' CssClass="hideControl"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Update" runat="server" OnClick="Update" />
</div>
<div>
<style type="text/css">
.hideControl
{
display: none;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=GridView1] tr td').on('click', function () {
$('[id*=GridView1] tr td').each(function () {
$(this).find('span').show();
$(this).find('input[type=text]').hide();
$(this).find('span').html($(this).find('input[type=text]').val());
});
var val = $(this).find('span').html();
$(this).find('span').hide();
$(this).find('input[type=text]').val(val);
$(this).find('input[type=text]').show();
$(this).find('input[type=text]').focus();
});
});
</script>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}
protected void Update(object sender, EventArgs e)
{
// code for update the record.
foreach (GridViewRow row in GridView1.Rows)
{
int id = int.Parse((row.FindControl("lblId") as Label).Text);
string name = (row.FindControl("txtName") as TextBox).Text;
string address = (row.FindControl("txtAddress") as TextBox).Text;
// call the update method.
UpdateCustomers(id, name, address);
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Address") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
private void UpdateCustomers(int id, string name, string address)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
SqlCommand cmd = new SqlCommand("UPDATE Customers SET Name = @Name, Address = @Address WHERE Id = @Id", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Address", address);
cmd.Parameters.AddWithValue("@Id", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindGrid()
End If
End Sub
Protected Sub Update(ByVal sender As Object, ByVal e As EventArgs)
' code for update the record.
For Each row As GridViewRow In GridView1.Rows
Dim id As Integer = Integer.Parse(CType(row.FindControl("lblId"), Label).Text)
Dim name As String = CType(row.FindControl("txtName"), TextBox).Text
Dim address As String = CType(row.FindControl("txtAddress"), TextBox).Text
' call the update method.
UpdateCustomers(id, name, address)
Next
End Sub
Private Sub BindGrid()
Dim dt As DataTable = New DataTable
dt.Columns.AddRange(New DataColumn() {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Address")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
End Sub
Private Sub UpdateCustomers(ByVal id As Integer, ByVal name As String, ByVal address As String)
Dim dt As DataTable = New DataTable
Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Dim cmd As SqlCommand = New SqlCommand("UPDATE Customers SET Name = @Name, Address = @Address WHERE Id = @Id", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Address", address)
cmd.Parameters.AddWithValue("@Id", id)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub