Question DataAdapter updates everything but the first DataGridView Record

leansoli

New member
Joined
Feb 15, 2011
Messages
2
Programming Experience
3-5
Hi!

I am new to .NET and been reading these forums in order to learn about it. So far, I must admit that I learned a lot. Hope to be able to share some knowledge in the future!

I have a question now, that I am unable to answer to myself.

I have a datagridview bound to my access DB (once I'm done learning access, I'll move to a more powerful DB). The problem I am facing is that when I call my Data Adapter Update method, it updates all the records in my DB but 'forgets' to update the first record of my DataGridView.

User is asked to enter a %age. Then I loop the grid, updating the values with that %age and once loop is done I ask whether he/she wants to reflect those changes to the DB. If Yes, why would my line: da.update(ds, "datos") fail to update the first record of my grid and not fail to update all the other records?

Thanks ni advance!
 
No, I'm perfectly aware of that ;)


Here is the code:
VB.NET:
Private Sub ModificaPrecios()
        Dim mensaje As String
        Dim variacion As Integer
        Dim i As Int32

        mensaje = "¿Esta seguro que desea "
        If rdbAumento.Checked Then
            mensaje = mensaje & "aumentar un "
        Else
            mensaje = mensaje & "descontar un "
        End If
        If txPorcentaje.Text.Length > 0 Then
            mensaje = mensaje & txPorcentaje.Text & " porciento de sus listas de precios?"
            If MessageBox.Show(mensaje, "Actualizo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
'I asked if user is sure to change the column
'rdbAumento states whether the column value is increased or decreased
                If rdbAumento.Checked Then
                    variacion = Convert.ToInt32(txPorcentaje.Text)
                    For i = 0 To gridPrecios.Rows.Count - 2
                        gridPrecios.Item(5, i).Value = (gridPrecios.Item(5, i).Value * (100 + variacion) / 100)
                    Next
                Else
                    variacion = Convert.ToInt32(txPorcentaje.Text)
                    For i = 0 To gridPrecios.Rows.Count - 2
                        gridPrecios.Item(5, i).Value = (gridPrecios.Item(5, i).Value * (100 - variacion) / 100)
                    Next
                End If
            End If
        End If
        UpdateGrilla()
    End Sub

    Private Sub UpdateGrilla()
        Dim cb As New OleDb.OleDbCommandBuilder(da)

'If txporcentaje.text is not null, then user increased or decreased ALL THE COLUMN with the For above. Else, The user just changed ONCE CELL MANUALLY
        If txPorcentaje.Text <> nothing Then
            If MsgBox("Desea guardar los cambios realizados?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "WARNING") = MsgBoxResult.Yes Then


'This line is failling. See below for description
                da.Update(ds, "datos")

                'Hubocambio se usa para meter datos a HistorialPrecios
                hubocambio = True
            End If
        Else
            If MsgBox("Save data?.", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "WARNING") = MsgBoxResult.Yes Then

'The odd thing that is killing me is that this line is NOT failing.
                da.Update(ds, "datos")

                'Hubocambio se usa para meter datos a HistorialPrecios
                hubocambio = True
            End If
        End If
        If hubocambio Then
            InsertaCambioPrecio()
        End If

    End Sub

    Private Sub InsertaCambioPrecio()
        'Some routine that saves a record as a log of what is being done.
    End Sub

da.update(ds, "datos") is used two times: When the user updates the whole column or when he/she changes one cell. The problem appears when updating the whole column. This method is not updating the first row of the grid.
 
gridPrecios.Rows.Count - 2 will miss a row out somewhere. If the grid contains 100 rows, you'll do rows 0 to 98, so row 99 is missed. Is this the row youre complaining about?
 
Other thought i had, you may not be calling EndEdit on your gridview, so the last change may not be persisted into the datatable. Generally editing data by going via the grid is a bad idea, you should be editing the datatable the grid is bound to rather than sending changes via the grid itself
 
Has this been solved yet?

I have a similar problem. When using dataadapter.update(dataset, "table") it updates all but the row that are selected, not the first row in particular, but any row that has the editing icon next to it.
In another form i did, in the same application, the same da.update method works just fine, without the missing single row problem...
 
Just a quick thought - are you making sure that the gird has updated the current row before doing the datase update? i.e.EndEdit ...
 
Back
Top