I have a datagrid and a means of inserting a record into it using separate fields and an insert button. When I insert a record, it goes to the last row of the datagrid, although its sorted position would be the first row. If the user then clicks the delete button (template column in the datagrid), the value returned for e.Item.DataSetIndex is the correct row index where the user clicked, as expected. However, if I try to get the row associated with this index, it always returns the one that would be in that position after sorting, not the one the user thinks he's clicking on. It seems like the dataset has already been sorted into the new order but the page gets rendered unsorted.
I either need a way to force the data to sort before rendering the page (during insert), or a way to get the right key value from the row that the user is clicking on (during delete). Any suggestions?
I either need a way to force the data to sort before rendering the page (during insert), or a way to get the right key value from the row that the user is clicking on (during delete). Any suggestions?
Code:
Private Sub DataGrid1_ItemCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.ItemCommand
Dim row(0) As DataRow
If e.Item.ItemType <> ListItemType.Header And _
e.Item.ItemType <> ListItemType.Footer Then
'row(0) = CType(e.Item.DataItem, DataRow)
row(0) = DataView1.Item(e.Item.DataSetIndex).Row
End If
If e.CommandName = "Delete" Then
Response.Write(row(0).Item("DOCNUM") & row(0).Item("PN"))
Qmsfdata._Table.FindByDOCNUMPN(row(0).Item("DOCNUM"), row(0).Item("PN")).Delete()
'Qmsfdata._Table.Item(e.Item.DataSetIndex).Delete() - this doesn't work b/c index into dataset is different from index into dataview
' Update the database.
Dim ws As New qms.localhost.qmsfsvc
ws.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim diffqmsfls As New qms.localhost.qmsf1
diffqmsfls.Merge(Me.Qmsfdata.GetChanges())
ws.DeleteQmsLink(diffqmsfls)
Qmsfdata.Merge(diffqmsfls)
DataGrid1.DataBind()
End If
If e.CommandName = "AttachFile" Then
If Not (attach_pn.Value = "") Then
Dim newRow As DataRow = Qmsflinkdata._Table.NewRow
newRow("DOCNUM") = Trim(row(0).Item("DOCNUM"))
newRow("PN") = attach_pn.Value
Qmsflinkdata._Table.Rows.Add(newRow)
' Update the database.
Dim ws As New qms.localhost.qmsfsvc
ws.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim diffqmslk As New qms.localhost.qmsflinks
diffqmslk.Merge(Me.Qmsflinkdata.GetChanges())
ws.InsertQmsLink(diffqmslk)
Qmsflinkdata.Merge(diffqmslk)
Qmsfdata.Merge(ws.GetQmsFiles())
DataGrid1.DataBind()
Dim ws2 As New qms.localhost.qmsfsvc
ws2.Credentials = System.Net.CredentialCache.DefaultCredentials
Qmsfdata.Merge(ws2.GetQmsFiles())
DataGrid1.DataBind()
End If