Hi all.
My problem is probably ignorance as to how the DGV works, anyway, the problem.

I have a bound datagridview which is used to collect user input. Data for 3 of the 7 fields is entered by looping through the rows and progmatically entering common data anfter a 'Save' Button is pressed:

For I = 0 To DG1.RowCount - 2
DG1.Rows(I).Cells(4).Value = TextBox1.Text 'Operator
DG1.Rows(I).Cells(5).Value = TextBox2.Text 'Date
DG1.Rows(I).Cells(6).Value = TextBox3.Text 'Time 
Next
DG1.Update

Prior to this loop I delete the last unused row if its not a new row:

DG1.CurrentCell = DG1.Item(0, DG1.RowCount - 2)
If DG1.CurrentCell.Value.ToString = "" Then
DG1.Rows.RemoveAt(DG1.RowCount - 2)
DG1.Update()
End If

At this point the DGV looks the way it should...All fields completed correctly and no un used ro except for the new row.

After saving :

Me.ItemTableTableAdapter.Update(Me.StockMoveDataSet.ItemTable)

The 'Operator', 'Date', and 'Time' are not saved to the database for the first row only

Can someone please help me.
Thanks
(Rate)

Dani AI

Generated

Most likely cause: the first row is still in edit mode (its changes never committed to the underlying DataTable) when you call your update. Programmatically setting cell values on a bound DataGridView does not always push those edits into the DataTable until the grid/binding finishes the edit. That explains why every other row saves but the first row does not.

Try forcing the edits to finish before you call your adapter update. Example sequence:

' commit any current cell/row edits and the binding-source edit
Me.Validate()
DataGridView1.EndEdit()
BindingSource1.EndEdit()

Calling both the grid's EndEdit and the BindingSource.EndEdit ensures the edited values are written into the DataTable so the TableAdapter will see them. See the DataGridView.EndEdit and BindingSource.EndEdit documentation for details: DataGridView.EndEdit and BindingSource.EndEdit.

If you prefer a more robust approach than writing to grid cells, set the values on the underlying DataTable rows before saving (that avoids focus/edit-state problems). For example, iterate the DataTable rows and assign Operator/Date/Time where ItemCode is present, then call EndEdit/Update.

Troubleshooting tips: use your DataTable.GetChanges() to see which rows are marked Added/Modified before update; check DataRow.RowState for the first row. Avoid calling AcceptChanges before a successful update (AcceptChanges clears row state and prevents Update from sending rows). For row deletions use the BindingSource or delete the DataRow (rather than fiddling with CurrentCell indices), and consider using DataColumn.DefaultValue or BindingSource.AddingNew to set defaults for new rows. These steps should address the symptom described and build on 's data-binding suggestion.

Recommended Answers

All 2 Replies

I highly recommend you look into data binding, as it will save you a lot of time and virtually all of the code you've listed above.

Data binding will allow you to bind data from the datatable to your datagridview. When this happens all you do is initiate a fill event to populate the entire grid, allow modifications if you want, and use an update command to save. It automates a lot of the process for you...

Good Luck!

Thanks for your reply.
I dont think I have explained myself to correctly if thats how you are interpreting my post.
Please let me explain the best I can. I apreciate your patience with newbies.
I have an input for form with 3 TextBoxes. These TextBoxes collect the common data ie. Operator, Date, and Time.
On the form is also a blank DGV displaying one line without data but bound. The operator enters an ItemCode, ItemDesription, OpeningBalance, and Location without seeing the other 3 fields - Operastor, Date, Time.
When btnSave is pressed, the procedure copies the Operator, Date, and Time to the appropriate fields if rows(I).cells(0) has an ItemCode entered. The procedure then goes on to delete the last row if it is unused and not a new row. All this works fine. Im sure there is an easier way to perform this task but this is the only way I was able to get it to work the way I wanted....trying to make the process 'idiot proof' for the user.
Now....when I save the data supplied to the DGV, the Operator, Date, and Time is not saved for the first line of the DGV only. Please understand I am very new to all this and am finding it all a bit thick on terminology.
I hope this explains it a bit better.

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.