I have a datagridview (bound) which works fine as long as the users "accepts" each cell they type in by pressing the tab key. However, when they click in another cell before pressing "tab", the value is visible in the cell on the screen but is not actually there.

What I mean with that, is that on the screen it shows for example '5' in the column, but when I check the cell value it shows as an empty cell (and a calculation based on this cell value does not see the value either).

I have tried validating the cell content (but as it is blank, there is nothing really to validate) and I have tried to set the cell value to blank if there is nothing in there as a cell leave event.

Any idea on why this is happening and how I can make sure that a cell value is "accepted" before the user moves out of the cell by a mouse click? If you try the same in say Excel, it "accepts" the value even if the user does not press tab or enter to get out of the cell.

Help appreciated, thanks very much.

Check the keypress event of the datagrid.

Just to add some more to this query, if I click on a cell on the same row, it will accept the value by using the mouse click in another cell.

If I click on a different row, the value I have just entered is NOT accepted.

The leave row event does the calculation, so when I click on a different cell on the same row first, and then click on a cell in the row below, the leave row event calculates the value correct based on both cells.

If I click straight on the row below, the last cell value is seen as blank.

Minimalist - when you say to check the keypress event, I am not sure what it will do in this event? I want to ensure that any value entered in the cell remains there when the user leaves the cell, no matter what.

From my testing so far, the value typed in will only remain in the cell if the users uses the tab key to leave the cell, or when the users clicks somewhere else but on the same row only.

If the user clicks on a different row directly after entering a value in the cell, the cell shows the value on the screen, but is actually blank in the datagridview itself.

What I want to achieve is that no matter where the user clicks that the cell value is remaining in the cell, and not just displayed without actually being there.

So what happen if you enter a value and move with the arrow keys?

Identical response. Use horizontal arrow keys on the same row and all is well, use a vertical one after entering a value in the cell loses the value as well, the same as if you clicked the mouse.

O.K I can't replicdate this behaviour. What you can do is insert this code and double click in different rows, different cells and see if the values in the cells are return to the debug window. If yes, there is a bug in your code or you need to use another event to calculate your rows. Using debugging your code and go with the mouse over yourvariables will also display the values.

Private Sub DataGridView1_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
        Debug.Print(DataGridView1.CurrentCell.Value.ToString)
    End Sub

Useful tip, thanks - will try that now.

The same result. If the cell value has just been typed, and nothing else is done except for double click on the cell, it displays a blank value.

If after entry I tab to the next cell, then arrow key back, and then double click on the cell it shows the value in there.

1) I would check that there is nothing in any of the 3 keypress events
2) I would move the calculations of the row somewher else
3) If nothing changes I would create a new datagrid on the form, input some values, use the double click and if this works O.K I would check if some of the properties are different to the datagrid you use.

No idea why, but adding the following in the cell leave event made it work:

dgTimeSheet.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)

Well if this works well done. Please mark the thread as solved. Thanks

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.