We have a simple datagrid where the first column contains a Vendor Number and the second is a read-only column containing the name of that vendor. Users can freely enter into the first column, and in the CellValidation event, we verify that the format of what they entered is correct and then lookup the number they entered to populate the second column with the name.
The function looks something like this:
e.ColumnIndex = 0
vendNum = e.Value
'Format the Vendor Number
vendNum = FormatVendorNumber(vendNum)
.dgvVendors.CurrentCell.Value = vendNum
'dgvVendors is our data grid
'Find the name of the vendor
e.Row.Cells(1).Value = GetVendorName(vendNum)
If a cell enters edit mode and the value is changed, then the value displayed in the cell is not updated with the formatted version. However, if a cell enters edit mode and then leaves edit mode with no changes, the formatted value from our function is applied. For example, if you were to add a new row and type "1" in the cell, our format function would convert that to "00001.00", and if you put a breakpoint in the above code, you would see that both Me.dgvVendors.CurrentCell.Value and the vendNum variable would reflect this formatted value. However, once processing completes, the cell will still read "1" in the UI.
Now, after having done that, if you click the same cell to enter edit mode and then click somewhere else, the same function above will run, only this time the cell in the UI will be updated to display "00001.00" properly. Likewise, if you edit that formatted value and remove a zero, the function will fail to update the value, but if after doing that, you click on the cell and then leave it without making any changes, it will be formatted again and the missing zero will be added back. The validating event handler can only update the cell value if that value was not actually changed during editing.
Note: The last line setting e.Row.Cells(1).Value to the Vendor Name always works. We've tried this syntax to apply our updated Vendor Number to Cells(0), but get the exact same results as using Me.dgvVendors.CurrentCell.Value.
Any idea what's going on here or how to correct it?