I have been working on a time clock application, and I'm trying to be able to select an employee from the DataGridView and populate their information into text boxes. I have done some research and what I have found works . . . sometimes. So sometimes when I click on an employee it will show their name in the box other times I have to click around alot to do it. Any suggestions on a better way to do this?

Here's my code:

  Private Sub dgvEmployee_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvEmployee.CellContentClick

        Dim dgvRow As DataGridViewRow
        For Each dgvRow In dgvEmployee.SelectedRows
            txtName.Text = dgvRow.Cells("Employee").Value.ToString

    End Sub

Selectedrow need you to select full row.
In datagridview properties, change datagridview SelectionMode to FullRowSelect.

Edited 3 Years Ago by Jx_Man

Why loop through the datagridview if you can use this.

txtName.Text = dgvEmployee.Rows(e.RowIndex).Cells("Employee").Value.ToString()

Thank you for your suggestions, but I'm still having the problem of it only working sometimes. :-(

Try using the CellClick event not CellContentClick event handler.

   Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvEmployee.CellClick
      TextBox1.Text = dgvEmployee.Rows(e.RowIndex).Cells(0).Value.ToString()
      TextBox2.Text = dgvEmployee.Rows(e.RowIndex).Cells(1).Value.ToString()
      TextBox3.Text = dgvEmployee.Rows(e.RowIndex).Cells(2).Value.ToString()
   End Sub

Thank you for all of your help!

I have decided for this particular application I would like a different form to be populated with the employees information and that is working like a dream! Thank you!

This question has already been answered. Start a new discussion instead.