Hi akhter,
You need to add a HiddenField to keep track of the rows you are editing.
Refer below example.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# Bind("CustomerId") %>'></asp:Label>
<asp:HiddenField ID="hfIsEdited" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' AutoPostBack="true"
OnTextChanged="OnTextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Bind("Country") %>' AutoPostBack="true"
OnTextChanged="OnTextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Update" runat="server" OnClick="OnUpdate" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT CustomerId, Name, Country FROM Customers";
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
}
protected void OnTextChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow;
HiddenField hfIsEdited = row.FindControl("hfIsEdited") as HiddenField;
hfIsEdited.Value = "Edited";
}
protected void OnUpdate(object sender, EventArgs e)
{
foreach (GridViewRow row in gvCustomers.Rows)
{
HiddenField hfIsEdited = row.FindControl("hfIsEdited") as HiddenField;
if (!string.IsNullOrEmpty(hfIsEdited.Value.Trim()))
{
Label lblCustomerId = row.FindControl("lblCustomerId") as Label;
TextBox txtName = (row.FindControl("txtName") as TextBox);
TextBox txtCountry = (row.FindControl("txtCountry") as TextBox);
// Your update code goes here.
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "UPDATE Customers SET Name = @Name, Country = @Country WHERE CustomerId = @CustomerId";
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@CustomerId", lblCustomerId.Text.Trim());
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
hfIsEdited.Value = string.Empty;
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT CustomerId, Name, Country FROM Customers"
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query, con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Using
End If
End Sub
Protected Sub OnTextChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, TextBox)).NamingContainer, GridViewRow)
Dim hfIsEdited As HiddenField = TryCast(row.FindControl("hfIsEdited"), HiddenField)
hfIsEdited.Value = "Edited"
End Sub
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvCustomers.Rows
Dim hfIsEdited As HiddenField = TryCast(row.FindControl("hfIsEdited"), HiddenField)
If Not String.IsNullOrEmpty(hfIsEdited.Value.Trim()) Then
Dim lblCustomerId As Label = TryCast(row.FindControl("lblCustomerId"), Label)
Dim txtName As TextBox = (TryCast(row.FindControl("txtName"), TextBox))
Dim txtCountry As TextBox = (TryCast(row.FindControl("txtCountry"), TextBox))
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "UPDATE Customers SET Name = @Name, Country = @Country WHERE CustomerId = @CustomerId"
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@CustomerId", lblCustomerId.Text.Trim())
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim())
cmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim())
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
hfIsEdited.Value = String.Empty
End If
Next
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Screenshot
The Form
Database record after update