How to use a datagridview to add/update/edit rows

J

JeremyGrand

I've read what's available here, but can't seem to make this work right. I'm
experimenting with components on a form, although I'd rather create the
pieces & assemble them in code, but that's another issue entirely.

I have a table in sql2k with an autoincrement column as primary index. It
also has a changeDate column with a trigger that updates to current datetime
when any column other than itself is changed.

Made a new form, followed Cor's tip & used Data to add a datasource.
Attached the bindingsource to the grid. To save the data, I'm doing this:

Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DataGridViewCellEventArgs) Handles
DataGridView1.RowLeave
Try
Me.Validate()
Me.MoveTicketCrossReferenceBindingSource.EndEdit()
Me.MoveTicketCrossReferenceTableAdapter.Update(Me.MyDataset.MoveTicketCrossReference)
Catch ex As Exception
End Try
end sub

I can now view & edit the data, most of the time. But,
- the trigger usually does not fire, although sometimes it updates every row
with an identical date.
- The grid does not refresh -- gotta exit & open it.
- The autoincrement field increments up every time you enter the (*) row &
leave it, resulting in gaps in the primary index.
- Me.MoveTicketCrossReferenceBindingSource.Current is not recognized as a
property. I'd like to look at it to see whether certain things need to be
done.
 
B

brennca

First off, I was struggling with entering a new row, and then updating
the database. Your code helped me fix that (RowLeave event). Thanks.
Regarding your issue, it sounds like you don't have a where clause in
your trigger that limits the date update to the current row. If all
rows are being updated to the current date, you need to limit the
update to the current row. For grid refresh, you'll need to trigger
this somehow in your form, I think. A refresh button perhaps. Or some
other form or control event that would trigger the refresh. Regarding
the autoincrement, I have a data grid view that is bound to a table
with an identity (autoincrement) column in a SQLExpress 2005 database.
I can click in the new row as much as I want, and event edit the
contents, but if I escape out, thereby canceling the insert, my
identity field does not increment. So that one I don't know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top