Ref:
HTML
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="dvGrid" style="padding: 10px; width: 550px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
CustomerID :
<asp:TextBox ID="txtCustomerId" runat="server" OnTextChanged="SearchCustomer" AutoPostBack="true" />
<br />
<hr />
<asp:GridView ID="GridView1" runat="server" Width="550px" AutoGenerateColumns="false"
Font-Names="Arial" Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green"
OnRowEditing="EditCustomer" OnRowUpdating="UpdateCustomer" OnRowCancelingEdit="CancelEdit"
PageSize="10">
<Columns>
<asp:TemplateField ItemStyle-Width="30px" HeaderText="CustomerID">
<ItemTemplate>
<asp:Label ID="lblCustomerID" runat="server" Text='<%# Eval("CustomerID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="100px" HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblContactName" runat="server" Text='<%# Eval("ContactName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtContactName" runat="server" Text='<%# Eval("ContactName")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="150px" HeaderText="Company">
<ItemTemplate>
<asp:Label ID="lblCompany" runat="server" Text='<%# Eval("CompanyName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCompany" runat="server" Text='<%# Eval("CompanyName")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
<AlternatingRowStyle BackColor="#C2D69B" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
Script
<script type="text/javascript" src="scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="scripts/jquery.blockUI.js"></script>
<script type="text/javascript">
function BlockUI(elementID) {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(function () {
$("#" + elementID).block({ message: '<table align = "center"><tr><td>' +
'<img src="images/loadingAnim.gif"/></td></tr></table>',
css: {},
overlayCSS: { backgroundColor: '#000000', opacity: 0.6, border: '3px solid #63B2EB'
}
});
});
prm.add_endRequest(function () {
$("#" + elementID).unblock();
});
}
$(document).ready(function () {
BlockUI("dvGrid");
$.blockUI.defaults.css = {};
});
</script>
Namespace
C#
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
private void BindData(string customerId)
{
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE CustomerId = @CustomerId", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue("@CustomerId", customerId);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
protected void SearchCustomer(object sender, EventArgs e)
{
this.BindData(this.txtCustomerId.Text.Trim());
}
protected void EditCustomer(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData(this.txtCustomerId.Text);
}
protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData(this.txtCustomerId.Text);
}
protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
{
string CustomerID = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblCustomerID")).Text;
string Name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtContactName")).Text;
string Company = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCompany")).Text;
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(constr, con))
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update customers set ContactName=@ContactName,CompanyName=@CompanyName " +
"where CustomerID=@CustomerID";
cmd.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = CustomerID;
cmd.Parameters.Add("@ContactName", SqlDbType.VarChar).Value = Name;
cmd.Parameters.Add("@CompanyName", SqlDbType.VarChar).Value = Company;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
BindData(this.txtCustomerId.Text);
}
}
}
VB.Net
Private Sub BindData(ByVal customerId As String)
Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers WHERE CustomerId = @CustomerId", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
cmd.Parameters.AddWithValue("@CustomerId", customerId)
Dim ds As DataSet = New DataSet()
da.Fill(ds)
GridView1.DataSource = ds
GridView1.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub SearchCustomer(ByVal sender As Object, ByVal e As EventArgs)
Me.BindData(Me.txtCustomerId.Text.Trim())
End Sub
Protected Sub EditCustomer(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
BindData(Me.txtCustomerId.Text)
End Sub
Protected Sub CancelEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
GridView1.EditIndex = -1
BindData(Me.txtCustomerId.Text)
End Sub
Protected Sub UpdateCustomer(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim CustomerID As String = (CType(GridView1.Rows(e.RowIndex).FindControl("lblCustomerID"), Label)).Text
Dim Name As String = (CType(GridView1.Rows(e.RowIndex).FindControl("txtContactName"), TextBox)).Text
Dim Company As String = (CType(GridView1.Rows(e.RowIndex).FindControl("txtCompany"), TextBox)).Text
Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(constr, con)
cmd.CommandType = CommandType.Text
cmd.CommandText = "update customers set ContactName=@ContactName,CompanyName=@CompanyName where CustomerID=@CustomerID"
cmd.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = CustomerID
cmd.Parameters.Add("@ContactName", SqlDbType.VarChar).Value = Name
cmd.Parameters.Add("@CompanyName", SqlDbType.VarChar).Value = Company
con.Open()
cmd.ExecuteNonQuery()
con.Close()
GridView1.EditIndex = -1
BindData(Me.txtCustomerId.Text)
End Using
End Using
End Sub
Screenshot