views:

344

answers:

3

I have a form with a list box and a few text boxes, when the user selects an item from the list box, i need the approptiate information to show up in the txt boxes and allow the user to edit it.

Here's my form Load event:

Private prt As New DataAccess.Part

Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

  Dim lc As System.Web.UI.WebControls.ListItemCollection = DataAccess.Part.GetListItems()
  cboParts.DataSource = lc
  lstParts.DataSource = lc

  txtPartBefore.DataBindings.Add(New Binding("Text", prt, "PartNumBefore", True, DataSourceUpdateMode.OnPropertyChanged))
  txtPartAfter.DataBindings.Add(New Binding("Text", prt, "PartNumAfter", True, DataSourceUpdateMode.OnPropertyChanged))
  txtOperation.DataBindings.Add(New Binding("Text", prt, "Operation", True, DataSourceUpdateMode.OnPropertyChanged))
  txtNotes.DataBindings.Add(New Binding("Text", prt, "Notes", True, DataSourceUpdateMode.OnPropertyChanged))

End Sub

And the SelectedIndexChange for the list box:

Private Sub lstParts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstParts.SelectedIndexChanged

 Dim ctl As ListControl = DirectCast(sender, ListControl)

 prt = DataAccess.Part.FetchByID(ctl.SelectedValue.value)

End Sub

I'm using VB.Net 2005, but can convert your suggestions from C# if needed ;)

Thanks Tony W

A: 

Your issue is the fact that you're assigning prt in your code. Your controls are being bound to whatever reference prt points to at the time the bindings are created. When you assign it later, the controls are not pointing to that.

What you should do is add a BindingSource component, have it point to the type of object that prt is, then bind your controls to the BindingSource component. When you need to retrieve, set the DataSource property of the BindingSource to be your retrieved object.

Adam Robinson
+1  A: 

You're not actually binding the newly Selected value to the text boxes. All you're doing is changing your reference (prt) to point from the original binding object, to a different object that was just selected by the user.

You need to do something like this:

    Private Sub lstParts_SelectedIndexChanged(
        ByVal sender As System.Object, ByVal e As System.EventArgs) 
        Handles lstParts.SelectedIndexChanged

        Dim ctl As ListControl = DirectCast(sender, ListControl)

        prt = DataAccess.Part.FetchByID(ctl.SelectedValue.value)

        BindControls(prt)
    End Sub

    Sub BindControls(ByVal newPart as DataAccess.Part)
        txtPartBefore.DataBindings.Clear()
        txtPartAfter.DataBindings.Clear()
        txtOperation.DataBindings.Clear()
        txtNotes.DataBindings.Clear()

        txtPartBefore.DataBindings.Add(
            New Binding("Text", newPart, "PartNumBefore", True, 
            DataSourceUpdateMode.OnPropertyChanged))
        txtPartAfter.DataBindings.Add(
            New Binding("Text", newPart, "PartNumAfter", True, 
            DataSourceUpdateMode.OnPropertyChanged))
        txtOperation.DataBindings.Add(
            New Binding("Text", newPart, "Operation", True, 
            DataSourceUpdateMode.OnPropertyChanged))
        txtNotes.DataBindings.Add(
            New Binding("Text", newPart, "Notes", True, 
            DataSourceUpdateMode.OnPropertyChanged))
    End Sub
Joseph
Taking this route does sortof defeats the "set it and forget it" mentality of databinding, though. It's better to bind to a persistent object one time rather than destroying and recreating the data bindings every time.
Adam Robinson
Not really, in the sense of how he's using it, databinding isn't responsible for knowing when the user intends to "change" what object is being databound itself. The problem is not the databinding, it's the intent to change what is databound.
Joseph
Awesome !thanksTony W
Tony W
A: 

You would bind to a property 'Prt':

public DataAccess.Part Prt { get { return prt; } set { prt = value; } }

or

Public Property Prt() As DataAccess.Part
Get
  Return prt
End Get
Set(ByVal Value As DataAccess.Part)
  prt = value
End Set
End Property
P a u l