Binding PictureBox to Datagrid problems

  • Thread starter Thread starter Chiragc
  • Start date Start date
C

Chiragc

Guest
Hi everyone, I have a bit of a problem in VB.net.
As some of you maybe aware, you cannot directly bind a datgrid to a picturebox in VB.net, so you have to use the work around as described in the MSDN. I managed to get my pictures displaying OK, but now I have a problem with binding specific records with their appropriate images.
I have 20 images in a database, which have been bound to the datagrid. However, if I perform a sort on these records, the images stay the same, i.e. the images are bound to the datagrid index instead of to their proper records. Therefore, I have a mismatch of images with the appropriate data.

I have been told to use a bindingManagerbase to solve this problem but dont know how to.
Here is the code I have used:

(In this code, I have two pictures displayed at a time, one to show the sport taking place, eg. Tennis, and the second showing the location of the venue.

Public Sub displaypicture()

Dim myBindingManagerBase As BindingManagerBase
Dim rows As Integer = DataGrid1.CurrentRowIndex

Dim pic1() As Byte = _
DataSet11.Tables("sport").Rows(rows)("sport_Image")
Dim sportpicholder As New MemoryStream(sportpic)
PictureBox1.Image = Image.FromStream(sportpicholder)
' This displays the sport picture
Dim pic2() As Byte = _
DataSet11.Tables("sport").Rows(rows)("location_Image")
Dim locationpicholder As New MemoryStream(locationpic)
PictureBox2.Image = Image.FromStream(locationpicholder)
'This displays the location picture
BindControl()

End Sub

Protected Sub BindControl()
' Create a Binding object for the PictureBox control.
' The data-bound property for the control is the image
' property.
Dim myBinding As New Binding("Image", DataGrid1, "sport_Image")
PictureBox1.DataBindings.Add(myBinding)

' Get the BindingManagerBase for the Customers table.
Dim bmEvents As BindingManagerBase = Me.BindingContext(DataGrid1, "sport")

' Add the delegate for the PositionChanged event.
AddHandler bmEvents.PositionChanged, AddressOf Position_Changed
End Sub 'BindControl


Private Sub Position_Changed(ByVal sender As Object, ByVal e As EventArgs)
' Print the Position property value when it changes.
Console.WriteLine(CType(sender, BindingManagerBase).Position)
End Sub 'Position_Changed


End Class

The line I am having problem with is
Dim myBinding As New Binding _
("Image" ,DataGrid1, "sport_Image")

I am using the bind property Image to set the image, but Dont know what I am doing wrong. Does anyone have any suggestions? Can anyone understand what I am trying to say?
Sorry If I have confused anyone!!



Thanx
 
Back
Top