Hi Guys / Gals

I am new to programming and am having some trouble with my code to update my data from a datagridview wich is bound to a datatable.

all works fine until the update proccess is started. The programs updates all my rows (x amount) on the datagridview but when it is saved the first rows data is reverted to what it was before update proccess was started..

EX.

My rows

ID Reg Brand
0 123 456
1 789 987

and i change those rows to

ID Reg Brand
0 321 654
1 987 789


then when it is saved / updated to the datatable it comes out like this

ID Reg Brand
0 123 456
1 987 789


The first row did not save the new data passed to the datagrid but all the other rows did save the data correctly...

How did you perform update ??? any piece of code for us ???

Hi ok what i did ....

Combobox activates query on datagridview. query get certain data from datagrid and only displays that data.

Now from a textbox i give it a new reg number. so the old one need to change to the new one.

so this is how i change the datagridview

If Tempreg <> reg Then
            For i = 0 To TripsDataGridView.RowCount - 1
                TripsDataGridView.Rows(i).Cells(1).Value = reg
            Next
        End If

Me.Validate()
Me.TripsTableAdapter.Update(Me.FCDBDataSet)

this works but not on the first line.

the reason for the(-1) in the "For i" is to ignore the openline that comes last in the datagrid view.

Hope this helps

Hi!

You could try like this: (update the datatable directly inside Dataset then bind with datagridview)

i have a table "tblCustomer" inside Dataset "FCDBDataSet", So:

For i As Integer = 0 To Me.FCDBDataSet.tblCustomer.Rows.Count - 1
            Me.FCDBDataSet.tblCustomer.Rows(i).Item(2) = reg  '3rd cell will update
        Next

        Me.TripsTableAdapter.Update(Me.FCDBDataSet.tblCustomer)  'update here
        Me.FCDBDataSet.tblCustomer.AcceptChanges()               'Accept changes 

        Me.DataGridView1.DataSource = Me.FCDBDataSet.tblCustomer 'Bind to DataGridView
commented: Very Helpfull +0

Thanks, I was away for the weekend and only got home now :-(. I am very Tired i will try this tomorrow and let you know if it worked.

Ok i checked it fast bcause it is important to me lol.

So it did not work and i checked my code again and again and came up with a workaround.

(Maybee i should have mentioned the datagridview is filled with a query)

But when i execute my save command so it updates the DGV and the DS it does not put in a new line. So (-1) was my problem. but is i make it (-0) I get

"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

So i made it (-1) again. and just added a new line at the back of my DGV. So having the (-1) takes out the line i just showed in there leaving no open spaces in my DT and updating the stuff i need. My code is as follow.

If Tempreg <> reg Then
            Me.TripsBindingSource.AddNew()

            For i = 0 To TripsDataGridView.RowCount - 1
                TripsDataGridView.Rows(i).Cells(1).Value = reg
            Next

End If

Me.TripsTableAdapter.Update(Me.FCDBDataSet.Trips)
Me.FCDBDataSet.Trips.AcceptChanges()

Thanks for the reply ShahanDev

It was helpfull at the end..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.