Hi Mehram,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="gvDetail" runat="server" AutoGenerateColumns="false" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="Rate" SortExpression="Rate">
<ItemTemplate>
<asp:TextBox ID="txtRate" runat="server" Text='<%# Bind("Rate") %>' Width="100px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:LinkButton ID="lnkSave" runat="server" Text="Save" OnClick="lnkSave_Click"></asp:LinkButton>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ID", typeof(int)),
new DataColumn("Item", typeof(string)),
new DataColumn("Rate",typeof(int)) });
dt.Rows.Add(1, "pen", 100);
dt.Rows.Add(2, "pencil", 150);
dt.Rows.Add(3, "Book", 255);
dt.Rows.Add(4, "Eraser", 55);
ViewState["dt"] = dt;
this.BindDetails();
}
}
public void BindDetails()
{
gvDetail.DataSource = (DataTable)ViewState["dt"];
gvDetail.DataBind();
}
protected void lnkSave_Click(object sender, EventArgs e)
{
DataTable dt = ViewState["dt"] as DataTable;
for (int index = 0; index <= gvDetail.Rows.Count - 1; index++)
{
GridViewRow row = gvDetail.Rows[index];
string InvDetID = gvDetail.DataKeys[index].Values[0].ToString();
string Amt1 = (gvDetail.Rows[index].FindControl("txtRate") as TextBox).Text;
DataRow dr = dt.Select("ID=" + InvDetID)[0];
dr["Rate"] = Amt1;
}
ViewState["dt"] = dt;
this.BindDetails();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("ID", GetType(Integer)), New DataColumn("Item", GetType(String)), New DataColumn("Rate", GetType(Integer))})
dt.Rows.Add(1, "pen", 100)
dt.Rows.Add(2, "pencil", 150)
dt.Rows.Add(3, "Book", 255)
dt.Rows.Add(4, "Eraser", 55)
ViewState("dt") = dt
Me.BindDetails()
End If
End Sub
Public Sub BindDetails()
gvDetail.DataSource = CType(ViewState("dt"), DataTable)
gvDetail.DataBind()
End Sub
Protected Sub lnkSave_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
For index As Integer = 0 To gvDetail.Rows.Count - 1
Dim row As GridViewRow = gvDetail.Rows(index)
Dim InvDetID As String = gvDetail.DataKeys(index).Values(0).ToString()
Dim Amt1 As String = (TryCast(gvDetail.Rows(index).FindControl("txtRate"), TextBox)).Text
Dim dr As DataRow = dt.[Select]("ID=" & InvDetID)(0)
dr("Rate") = Amt1
Next
ViewState("dt") = dt
Me.BindDetails()
End Sub
Screenshot
![](https://i.imgur.com/e5Hv9IM.gif)