DataGridView edits

M

Michael Barrett

Version: VS 2008, VB.Net

I have a Sub routine firing on a DataGrisView using either a doubleclick or
a key press. There are 10 columns, and it updates one of the columns by
incrementing it a specific value. That works fine.

The problem is the user has an option to do an incremental save. If they use
a key press or doubleclick to update the row and they try to save without
leaving the row, it doesn't recognize the new value.

I am pretty new to this control for data edits. Could someone help me out
with some code for this issue? What would you suggest to guarantee the row
edits are committed while it still has focus?

I tried BeginEdit/CommitEdit, but when using a key press it only works the
first one, then the cell gets focus, and the key press strokes appear in the
cell, which makes a mess.

I am sure I am overlooking something silly or simple. Thanks in advance for
any help.

Mike
 
C

Cor Ligthert[MVP]

Michael,

A row change pushes the data in the datagridview to the underlaying (data)
row

This can be forced by .endedit

Cor
 
C

Cor Ligthert[MVP]

Hi,

I forgot to tell, as you have updated a row in your database, then the
timestamp is not the same anymore as it was.

Therefore you need as you do the currency with a timestamp a refill of your
datasource.

if you are using an autoidentiefier, then a new row will need an insert,
while the second time you need an update.
This can give a lot of problem, therefore in those cases a fill can also be
done.

However, this can mean that the representing DataGridView gets other data
and therefore has changes.

Cor
 
M

Michael Barrett

Thank you for the reply. I'd like to expand a bit on my situation.

The grid is bound to an editable DataTable (datagrid.datasource = datatable)

On a KeyPress (+) or row DoubleClick, the following sub fires:

Sub AddQuantity()
Try
Dim dgvr As DataGridViewRow
dgvr = dgvOrderPad.CurrentRow

Dim i As Integer = dgvr.Cells.Item("Qty").Value
Dim x As Integer = dgvr.Cells.Item("UOS").Value
dgvr.Cells.Item("Qty").Value = i + x

Catch ex As Exception
MsgBox(Ex.Message)
End Try
End Sub

The user also has the option to click directly into the Qty column and enter
a value.

When they move off a row, the changes commit, so all is well. If they don't
move off the row and save, not only doesn't teh new value commit, but it now
basically reverts to the original value underneath, making the onscreen
value inaccurate.

I tried using BeginEdit/CommitEdit/EndEdit. Maybe I did something wrong, but
the problem I had was that multiple KeyPress (+) events would only register
the first, then start putting + signs in the editable cell. Obviously, I am
doing something incorrectly.

My goal is to commit the edit to the underlying DataTable after each event
without changing rows. Can I do that?

Thanks a ton.
Mike B
 
M

Michael Barrett

I got it to work with this code:

dgvOrderPad.CurrentRow.DataGridView.EndEdit()
dgvOrderPad.EndEdit()
OrderPadDataTable.AcceptChanges()

Its probably what you told me teh first time, but I can be a little thick.

Thanks for the help.

Mike
 
C

Cor Ligthert[MVP]

Michael,

I think that the one in this link is the easiest solution for you.

http://www.vb-tips.com/dbpages.aspx?Search=expression

To understand it uncomment first the line

dt.Columns("USA").ColumnMapping = MappingType.Hidden

That is not for your problem, I just did two things in one sample.

You can use a datagridview instead of a datagrid.

Cor
 

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