How do I change a datarow in a Gridview?

burak

Centurion
Joined
Jun 17, 2003
Messages
127
Hi,

In my gridview, I have two BoundField columns and a templatefield column as follows

<asp:GridView>
<Columns>
<asp:BoundField DataField="ams" HeaderText="Ams" ReadOnly="True"/>
<asp:BoundField DataField="ams_desc" HeaderText="Desc"
ReadOnly="True"/>
<asp:TemplateField HeaderText="AmsTest" ItemStyle-Wrap="true" HeaderStyle-Width="120">
<ItemTemplate>
<asp:TextBox ID="txtStudents" Text='<%# Eval("val") %>'
AutoPostBack="true"
OnTextChanged="HandleTextChange" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<asp:SQLDataSource ID="sqlProducts" Runat="Server"
SelectCommand = "SELECT ams, ams_desc From audit_ams"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>">
</asp:SQLDataSource>

when a user changes the value in the textbox and presses enter, I call this event and change the values in the first two columns

Public Sub HandleTextChange(ByVal sender As Object, ByVal e As System.EventArgs)

Dim x As TextBox
x = CType(sender, TextBox)
Dim y As System.Web.UI.WebControls.DataControlFieldCell
y = CType(x.Parent, System.Web.UI.WebControls.DataControlFieldCell)
Dim z As System.Web.UI.WebControls.GridViewRow
z = CType(y.Parent, System.Web.UI.WebControls.GridViewRow

z.cells(0).text = "some value" -> changes the first boundfield column
z.cells(1).text = "other value" -> changes the second boundfield column
End Sub

but when I move to a different page, I lose the values, since the change was not made to the underlying datatable.

I tried to get the underlying datarow as follows

Dim f As DataRow
f = CType(z.DataItem, Data.DataRow)

but nothing happened.

How can I change the underlying datarow for this GridViewRow?

Thanks,

Burak
 
Rather than read from the database straight into the gridview, you could always read the data into a dataset or datatable, then assign that to the gridview.

However i'd usually change the underlying data in the sites i've created, then just re-read it from the original datasource.
 
Back
Top