views:

32

answers:

2

I have a gridview with 3 columns, only one column is going to be edited by the user. Whenever it is edited I'd like to set one of the other columns to the current time. A "time last updated" if you will. Possible?

A: 

Add an event handler to the grid view like so

Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
     If e.columnIndex > lowerBound AndAlso e.columnIndex < UpperBound Then
          dataGridView1.item(columnNumberWithTimes, e.rowIndex).value = Date.Now
     End If
 End Sub

This method assumes that there is a range of columns that can be edited, defined by lowerBound and upperBound. Now, whenever a cell is modified, if that cell lies within that range, a cell in that respective row in a column defined by columnNumberWithTimes will be updated with the new time.

AndyPerfect
Do I have to import anything for this to work? VS isn't recognizing any of this.
Shawn
No, you shouldn't need to import anything. Just make sure you change your variable names? DataGridView1 should be replaced with the name of your Grid. All other variables would need to be defined elsewhere or at the beginning of the method to work, if that happens to be the problem.
AndyPerfect
+1  A: 

Andy's solution works.

Another method would be to alter the UPDATE sql statement that is associated with the grid. Use GetDate() (or your DB equivalent) in the UPDATE statement like so:

UPDATE MyTable SET usereditvalue = @usereditvalue, mytimestamp = GETDATE()

Optionally with a Where statement:

WHERE MyTable.ID = @oldIDvalue

For doing it this way, read up on parameterized queries and gridview / table keyvalues (for your WHERE statement).

Tobiasopdenbrouw
Much easier to implement imo, ty.
Shawn