Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserted record in wrong sort order

Status
Not open for further replies.

jartman

Programmer
Oct 16, 2001
34
US
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?

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
 
'I would grab a rowid by using:
Dim RowID = DataBinder.Eval(e.Item.DataItem,"DOCNUM")
'Assuming DOCNUM is your row id, if not use the correct one.

'execute your sql
sql = "Delete from MyTable where DOCNUM = " & RowID
'....


'After you deleted your record above. Execute your code for getting a new dataset.
dim ds as new dataset
MyDataAdapter.Fill(ds)

'Get a dataview to sort.
CheckDataGridPaging(DataGrid1,ds.tables("MyTable"))
Dim dv as DataView=ds.tables("MyTable").DefaultView
'Set my sort column
dv.sort="DOCNUM"
DataGrid1.DataSource=dv
DataGrid1.DataBind()

'Add CheckDataGridPaging to your utils for all projects
Your DataGrid.CurrentPageIndex can end up being a higher number than your new dataset has. This happens when your previous dataset contained 20 pages, your CurrentPageIndex is still set at 20, but your new DataSet with your deleted rows has only 19 pages.








Public Sub CheckDataGridPaging(ByVal dg As DataGrid, ByVal tbl As DataTable)
Dim rws As Integer = tbl.Rows.Count
If rws = 0 Then
dg.CurrentPageIndex = 0
Exit Sub
End If
Dim pages As Integer = Int(rws / dg.PageSize)
If (rws / dg.PageSize) > Int(rws / dg.PageSize) Then pages = pages + 1
Do
If (dg.CurrentPageIndex + 1) > pages Then
If dg.CurrentPageIndex = 0 Then Exit Sub
dg.CurrentPageIndex = dg.CurrentPageIndex - 1
Else
Exit Sub
End If
Loop
End Sub




 
RTomes -

Thanks for the attempt, but it doesn't solve the problem. DataBinder.Eval(e.Item.DataItem,"DOCNUM") will still return a different DOCNUM than the one the user sees. I'll try to describe the situation more clearly.

Let's say there are 3 records in the datagrid with DOCNUMs "BETA", "GAMMA", "DELTA". If the user inserts a record with DOCNUM = "ALPHA", he sees the records in order "BETA","GAMMA","DELTA","ALPHA". But the records in the dataset are ordered "ALPHA","BETA","GAMMA","DELTA". Here is the problem - the order of records in the rendered datagrid does not match that in the underlying dataset, even after DataBind! So if the user then clicks the delete button on the 4th row (intending to delete ALPHA), DataBinder.Eval will return "DELTA".

If the user refreshes the page before deleting (by navigating away from the page and back again), the datagrid shows the records in sorted order and the problem goes away.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top