How To DataGridView DataBoundItem VB2005

This article and source code demonstrate how to bind objects stored in a generic List (Of T) to a DataGridView and how to retrieve an individual object when a row header of the DataGridView is double-clicked. The DataGridView Row’s DataBoundItem property is used to retrieve the object that is bound to row.

The DataGridView, new in Visual Basic 2005, provides many new members for displaying and manipulating data. One member, the DataGridView Row’s DataBoundItem, makes it each to ‘get at’ the data behind a DataGridView row. This member is especially useful when working with a data source such as a generic List (Of T) object.

Example Application Screenshot

Source Code Excerpt

 

Private Sub PhotosDataGridView_RowHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles PhotosDataGridView.RowHeaderMouseClick

    ‘ Use the DataBoundItem property of a DataGridView Row to retrieve

    ‘ the data object bound to the row.

    Dim selectedPhoto As Photo = CType(Me.PhotosDataGridView.Rows(e.RowIndex).DataBoundItem, Photo)

 

    ‘ Display the photo title, description, and image.

    Me.TitleLabel.Text = selectedPhoto.Title

    Me.DescriptionLabel.Text = selectedPhoto.Description

    Me.PhotoPictureBox.Image = Image.FromFile(selectedPhoto.PathToPhoto)

End Sub

Advertisements

~ bởi duriangroup on Tháng Sáu 1, 2007.

 
%d bloggers like this: