I have prepared a simple example to help deal with some of the issues
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="spexample.aspx.vb" Inherits="spexample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="OrderID,Expr1,ProductID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False"
ReadOnly="True" SortExpression="OrderID" />
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID"
SortExpression="CustomerID" />
<asp:BoundField DataField="OrderDate" HeaderText="OrderDate"
SortExpression="OrderDate" />
<asp:BoundField DataField="RequiredDate" HeaderText="RequiredDate"
SortExpression="RequiredDate" />
<asp:BoundField DataField="ShippedDate" HeaderText="ShippedDate"
SortExpression="ShippedDate" />
<asp:BoundField DataField="Expr1" HeaderText="Expr1" ReadOnly="True"
SortExpression="Expr1" />
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True"
SortExpression="ProductID" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
SortExpression="UnitPrice" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity"
SortExpression="Quantity" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT Orders.OrderID, Orders.CustomerID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate, [Order Details].OrderID AS Expr1, [Order Details].ProductID, [Order Details].UnitPrice, [Order Details].Quantity FROM Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID"
UpdateCommand="UPDATE [Order Details] SET ProductID = ProductID, Quantity = Quantity, UnitPrice = UnitPrice">
</asp:SqlDataSource>
</div>
</form>
</body>
</html> Partial Class spexample
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
' this is the ideal place to grab data after an edit and before committing
' to the database. In this example I am pulling Unitprice and quantity to do some
' calculations
Dim unitprice As Decimal = e.NewValues("UnitPrice")
Dim quantity As Decimal = e.NewValues("Quantity")
Dim extended As Decimal = unitprice * quantity
' The above works because I am binding with a SqlDataSource control but If I use
' Dataset and use DataSource property the above will not work in that case
Dim uprice As Decimal = Convert.ToDecimal(CType(GridView1.Rows(e.RowIndex).Cells(8).Controls(0), TextBox).Text)
Dim qty As Decimal = Convert.ToDecimal(CType(GridView1.Rows(e.RowIndex).Cells(9).Controls(0), TextBox).Text)
Dim ext As Decimal = uprice * qty
' FindControl usually only works if you have template fields but in the above case I stuck with standard bound
' fields and we really don't know what the textbox name are or if it is even possible.
' At this point you can override the normal update mechanism and use own data access. If you did and you were
' using SqlDataSource control then you need to abort the command to update
e.Cancel = True
' because this will abort the update it also leaves the grid in edit mode. to deal with this do
GridView1.EditIndex = -1
GridView1.DataBind()
End Sub
End Class