Hi imoleayoemman...,
You need to keep the data in DataTable. Then on TextBox change update the DataTable and use that DataTable to populate the GridView.
Refer below sample.
HTML
<asp:GridView runat="server" ID="gvOrders" AutoGenerateColumns="false"
AllowPaging="true" PageSize="2" OnPageIndexChanging="OnPageIndexChanging">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Item" HeaderText="Item" />
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:BoundField DataField="Amount" HeaderText="Amount" />
<asp:TemplateField HeaderText="Amount Applied">
<ItemTemplate>
<asp:TextBox ID="txtAmountApplied" runat="server" Text='<%#Eval("AmountApplied") %>'
AutoPostBack="true" OnTextChanged="OnTextChanged" Width="100px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetData();
this.BindGridView();
}
}
private void GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Id"),
new DataColumn("Item"),
new DataColumn("Date"),
new DataColumn("Amount"),
new DataColumn("AmountApplied")
});
dt.Rows.Add(1, "T-Shirt", "07-09-2012", "595.46", "");
dt.Rows.Add(2, "Pant", "07-09-2013", "48.29", "");
dt.Rows.Add(3, "Football", "07-13-2013", "595.46", "");
dt.Rows.Add(4, "Bat", "07-13-2013", "48.29", "");
ViewState["Orders"] = dt;
}
private void BindGridView()
{
gvOrders.DataSource = (DataTable)ViewState["Orders"];
gvOrders.DataBind();
}
protected void OnTextChanged(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["Orders"];
GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow;
int id = Convert.ToInt32(row.Cells[0].Text);
DataRow dr = dt.AsEnumerable().Where(x => Convert.ToInt32(x["Id"]) == id).FirstOrDefault();
if (dr != null)
{
dr["AmountApplied"] = (sender as TextBox).Text;
}
ViewState["Orders"] = dt;
this.BindGridView();
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvOrders.PageIndex = e.NewPageIndex;
this.BindGridView();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.GetData()
Me.BindGridView()
End If
End Sub
Private Sub GetData()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("Id"),
New DataColumn("Item"),
New DataColumn("Date"),
New DataColumn("Amount"),
New DataColumn("AmountApplied")
})
dt.Rows.Add(1, "T-Shirt", "07-09-2012", "595.46", "")
dt.Rows.Add(2, "Pant", "07-09-2013", "48.29", "")
dt.Rows.Add(3, "Football", "07-13-2013", "595.46", "")
dt.Rows.Add(4, "Bat", "07-13-2013", "48.29", "")
ViewState("Orders") = dt
End Sub
Private Sub BindGridView()
gvOrders.DataSource = CType(ViewState("Orders"), DataTable)
gvOrders.DataBind()
End Sub
Protected Sub OnTextChanged(sender As Object, e As EventArgs)
Dim dt As DataTable = CType(ViewState("Orders"), DataTable)
Dim row As GridViewRow = TryCast((TryCast(sender, TextBox)).NamingContainer, GridViewRow)
Dim id As Integer = Convert.ToInt32(row.Cells(0).Text)
Dim dr As DataRow = dt.AsEnumerable().Where(Function(x) Convert.ToInt32(x("Id")) = id).FirstOrDefault()
If dr IsNot Nothing Then
dr("AmountApplied") = (TryCast(sender, TextBox)).Text
End If
ViewState("Orders") = dt
Me.BindGridView()
End Sub
Protected Sub OnPageIndexChanging(sender As Object, e As GridViewPageEventArgs)
gvOrders.PageIndex = e.NewPageIndex
Me.BindGridView()
End Sub
Screenshot