Reputation: 75
I am using a DataGridView
and I am attempting to auto populate a value into a cell based on what a user enters into the previous cell. There will be a query to the DB to capture the appropriate value to be autofilled.
Example:
User enters "1234" into Cell 1 -- Automatically, "5678" is entered into Cell 2.
How can I accomplish this?
I tried something like this and it did not work.
Dim X As String
X = dataGridView3.CurrentRow.Cells(1).Value.ToString
If X = "1234" Then
dataGridView3.CurrentRow.Cells(2).Value = "5678"
End If
Upvotes: 0
Views: 111
Reputation: 7465
The DataGridView CellValueChanged Accomplishes this:
Private Sub CellValueChanged(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellValueChanged
' Update the balance column whenever the values of any cell changes.
If e.RowIndex >= 0 and e.ColumnIndex == 1 And Not dataGridView1.Rows(e.RowIndex).Cells(1).Value Is Nothing AndAlso dataGridView1.Rows(e.RowIndex).Cells(1).Value.ToString() == "1234" Then
dataGridView1.Rows(e.RowIndex).Cells(2).Value == "5678"
End If
End Sub
Upvotes: 1