Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB Getting Object from DataGridView Row

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

like image 667
Zack Avatar asked May 23 '26 18:05

Zack


1 Answers

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
like image 159
Rhys Jones Avatar answered May 26 '26 09:05

Rhys Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!