Hi kankon,
Check this example. Now please take its reference and correct your code.
HTML
<asp:HiddenField ID="hfName" runat="server" />
<asp:HiddenField ID="hfAddUpdate" runat="server" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText="No records has been added."
OnRowEditing="OnRowEditing" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Edit" runat="server" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton Text="Update" runat="server" OnClick="OnUpdate" />
<asp:LinkButton Text="Cancel" runat="server" OnClick="OnCancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
<tr>
<td style="padding-bottom: 10px">Name:<br />
<asp:TextBox ID="txtName" runat="server" />
</td>
</tr>
<tr>
<td style="padding-bottom: 10px">Country:<br />
<asp:TextBox ID="txtCountry" runat="server" />
</td>
</tr>
<tr>
<td style="width: 100px">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="OnInsert" />
</td>
</tr>
</table>
Namespaces
C#
using System.Data;
using System.Drawing;
VB.Net
Imports System.Data
Imports System.Drawing
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("Country") });
ViewState["dt"] = dt;
this.BindGrid();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text.ToString().Trim() == hfName.Value.Trim())
{
if (hfAddUpdate.Value == "Added")
{
e.Row.BackColor = Color.DeepSkyBlue;
}
if (hfAddUpdate.Value == "Updated")
{
e.Row.BackColor = Color.Red;
}
}
}
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void BindGrid()
{
GridView1.DataSource = (DataTable)ViewState["dt"];
GridView1.DataBind();
}
protected void OnInsert(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
dt.Rows.Add(txtName.Text.Trim(), txtCountry.Text.Trim());
ViewState["dt"] = dt;
hfAddUpdate.Value = "Added";
hfName.Value = txtName.Text.Trim();
this.BindGrid();
txtName.Text = string.Empty;
txtCountry.Text = string.Empty;
}
protected void OnUpdate(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string name = (row.Cells[0].Controls[0] as TextBox).Text;
string country = (row.Cells[1].Controls[0] as TextBox).Text;
DataTable dt = ViewState["dt"] as DataTable;
dt.Rows[row.RowIndex]["Name"] = name;
dt.Rows[row.RowIndex]["Country"] = country;
ViewState["dt"] = dt;
hfAddUpdate.Value = "Updated";
hfName.Value = name;
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void OnCancel(object sender, EventArgs e)
{
hfAddUpdate.Value = "";
hfName.Value = "";
GridView1.EditIndex = -1;
this.BindGrid();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Name"), New DataColumn("Country")})
ViewState("dt") = dt
Me.BindGrid()
End If
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(0).Text.ToString().Trim() = hfName.Value.Trim() Then
If hfAddUpdate.Value = "Added" Then
e.Row.BackColor = Color.DeepSkyBlue
End If
If hfAddUpdate.Value = "Updated" Then
e.Row.BackColor = Color.Red
End If
End If
End If
End Sub
Protected Sub OnRowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
Me.BindGrid()
End Sub
Protected Sub BindGrid()
GridView1.DataSource = CType(ViewState("dt"), DataTable)
GridView1.DataBind()
End Sub
Protected Sub OnInsert(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = CType(ViewState("dt"), DataTable)
dt.Rows.Add(txtName.Text.Trim(), txtCountry.Text.Trim())
ViewState("dt") = dt
hfAddUpdate.Value = "Added"
hfName.Value = txtName.Text.Trim()
Me.BindGrid()
txtName.Text = String.Empty
txtCountry.Text = String.Empty
End Sub
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, LinkButton)).NamingContainer, GridViewRow)
Dim name As String = (TryCast(row.Cells(0).Controls(0), TextBox)).Text
Dim country As String = (TryCast(row.Cells(1).Controls(0), TextBox)).Text
Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
dt.Rows(row.RowIndex)("Name") = name
dt.Rows(row.RowIndex)("Country") = country
ViewState("dt") = dt
hfAddUpdate.Value = "Updated"
hfName.Value = name
GridView1.EditIndex = -1
Me.BindGrid()
End Sub
Protected Sub OnCancel(ByVal sender As Object, ByVal e As EventArgs)
hfAddUpdate.Value = ""
hfName.Value = ""
GridView1.EditIndex = -1
Me.BindGrid()
End Sub
Screenshot