Shawn
Shawn

Reputation: 2366

Modify gridview update action to put time in column?

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?

Upvotes: 0

Views: 786

Answers (2)

Tobiasopdenbrouw
Tobiasopdenbrouw

Reputation: 14039

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).

Upvotes: 1

AndyPerfect
AndyPerfect

Reputation: 1180

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.

Upvotes: 0

Related Questions