简体   繁体   中英

How to capture cell value change in datagridview in vb

My code gives error when I click update button after I make changes to the value in the cell. I am trying to capture user id and date whenever there is a change in cell value in a data row. Please advise.

Private Sub DataGridView4_CellValuechanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView4.CellValueChanged

    Dim counter As Integer
    UName = Environment.UserName
    Dim now As DateTime

     If e.ColumnIndex > 0 Then
       For counter = 0 To (DataGridView4.Rows.Count - 1)
            DataGridView4.Item(e.ColumnIndex, counter).Value = DataGridView4.Item(e.ColumnIndex, e.RowIndex).Value
            DataGridView4.Item("Modifieddate", e.RowIndex).Value = Date.Now
            DataGridView4.Item("ModifierID", e.RowIndex).Value = UName.ToString.ToUpper

        Next
    End If

End Sub

I am not sure what the If e.ColumnIndex > 0 Then is about. If it is a column that you don't want the user to edit then set the Column.ReadOnly = True and get rid of the If statement.

If you simply want to record the date and user who changed a cell in particular row.

 Private Sub DataGridView4_CellValuechanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView4.CellValueChanged
    If e.ColumnIndex > 0 Then
        DataGridView4.Item("Modifieddate", e.RowIndex).Value = Date.Now
        DataGridView4.Item("ModifierID", e.RowIndex).Value = Environment.UserName.ToUpper
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM