This is not as easy as you may think (or want it to be)...
The row index of the row in the DataGrid that you are editing is not
necessarily the same row index in your DataSet. What makes matters a bit
more confusing is that if you allow paging and/or sorting in your grid, the
2 index values may be way off from each other.
You must essentially query the dataset using some unique data from the row
being edited in the DataGrid (primary key) to locate the row of the dataset
that matches the row being edited in the DataGrid.
In addition, if you are using bound controls to show the row data in the
DataGrid, you can only grab those control values by asking for the control
by name. So, this code is an example of how to get the row in the DataSet
that matches the row in the DataGrid being edited:
Dim theRowToUpdate As DataRow = _
ds.Tables(0).Select("PartNumber='" & _
CType(e.Item.FindControl("lblPartNum"), Label).Text & "'")(0)
This example assumes you have a column in the DataSet named "PartNumber" and
a label in the DataGrid called "lblPartNum" that contains Primary Key
information about the record being edited. The "FindControl" method returns
an array, so at the end of the expression, I have "(0)" because I want the
first (and when using Primary Key data) and only row found.
You must also cast the control that is found into the correct type because
"FindControl" will only return objects and you'll want to extract the bound
data from the control via its bound property, in this case "Text".
As you can see, I am finding the row that contains a record with a primary
key value that matches the primary key data displayed in the grid. I am
then making a pointer to that row for easy access to it going forward.
Now, to update the rest of the data in the DataSet row, you'd need something
like this:
With theRowToUpdate
.Item("Name") = DirectCast(e.Item.FindControl("txtName"),
TextBox).Text
.Item("MFG") = DirectCast(e.Item.FindControl("txtMFG"), TextBox).Text
.Item("WholesalePrice") =
CType(DirectCast(e.Item.FindControl("txtWP"), TextBox).Text, Decimal)
.Item("RetailPrice") = CType(DirectCast(e.Item.FindControl("txtRP"),
TextBox).Text, Decimal)
.Item("OnHand") = CType(DirectCast(e.Item.FindControl("txtOnHand"),
TextBox).Text, Integer)
.Item("PicURL") = DirectCast(e.Item.FindControl("txtPicURL"),
TextBox).Text
.Item("Style") = DirectCast(e.Item.FindControl("txtStyle"),
TextBox).Text
.Item("Description") =
DirectCast(e.Item.FindControl("txtDescription"), TextBox).Text
End With
Now, after your DataSet is updated you need to call
DataAdapter.Update(dataSet) to get the original datasource updated and you
need to rebind your grid.