What I am missing ... windows form a binding with subsonic

I have a form with a list box and multiple text boxes, when the user selects an item from the list, I need the relevant information to display in the txt boxes and allow the user to edit it.

Here's my Load event form:

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:

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 sentences from C # if needed;

Thanks Tony W

+1


a source to share


3 answers


You are not actually binding the new Selected value to the text fields. All you do is change your link (prt) to a point from the original anchor object to another object that was 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

      

+1


a source


Your problem is what you are assigning prt

in your code. Your controls are bound to any link prt

pointing at the time the bindings were created. When you assign it later, the controls don't indicate this.



What you need to do is add a component BindingSource

, specify whether it's an object of type prt

, and then bind controls to the component BindingSource

. When you need to get, set the property to the DataSource

value of BindingSource

your restored object.

0


a source


You bind to the "Prt" property:

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

      

0


a source







All Articles