2
votes

First off, I found a similar question here, but don't understand how it works, and I'm not comfortable 'bumping' or replying to a couple month old question.

I have a datagridview control and it's DataSource property is set to a List type object.

Question: I'm trying to figure out how to reference the actual object of a selected row so I can display extra data about that object.

I've got the SelectionChanged event hooked up from the DataGrid View. The object is a reference to the row/computer selected on the DGV.

Private Sub LabUsersList_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxLabUsersList.SelectionChanged
    Dim selectedComputer = Me.uxLabUsersList.SelectedRows(0)
End Sub

Update: I'm seeking the underlying object from the list element, not the list element (DataSource).

Thanks in advance for the help :)

1
Do you mean you want the list element that has been databound to the row>? - John Saunders
no, the underlying object of the list element. I figured out from that other question how to reference the list element. - Zack

1 Answers

6
votes

Once you have the collection of SelectedRows (each element in the collection is a DataGridViewRow obect), you can get the underlying data bound item using the DataBoundItem property for each row.

In your case, try the following:

Private Sub LabUsersList_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxLabUsersList.SelectionChanged    
    Dim selectedComputer = Me.uxLabUsersList.SelectedRows(0).DataBoundItem
End Sub