如何从绑定源中删除对象不影响列表中的对象

问题描述:

我有2个数据成员的类供应商SupID和供应商名称和1构造函数然后我将此对象添加到Form1加载()时供应商列表。如何从绑定源中删除对象不影响列表中的对象

Dim lst As New List(Of Supplier) 

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

    Public Sub AddList() 
     lst.Add(New Supplier("Sup1", "Supplier1")) 
     lst.Add(New Supplier("Sup2", "Supplier2")) 
     lst.Add(New Supplier("Sup3", "Supplier3")) 
     lst.Add(New Supplier("Sup4", "Supplier4")) 
     lst.Add(New Supplier("Sup5", "Supplier5")) 
    End Sub 

然后我想通过它的新的即时构造函数来发送LST到窗体2,当我点击发送按钮:

Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click 
     Dim frm As New Form2(lst) 
     frm.Show(Me) 
    End Sub 

接着在窗口2,这样的代码:

Dim lst As New List(Of Supplier) 
    Dim bs As BindingSource 

    Public Sub New(ByVal lst As New List(Of Supplier) 
     Me.InitializeComponent() 
     Me.lst = lst 
     bs = new BindingSource(lst,nothing) 
    End Sub 

而且那么我想从bs中删除物体:

Public Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click 
     bs.RemoveCurrent() 
    End Sub 

问题是,当我从Form2中的bs中删除对象时,Form1中的lst中的对象也受到影响。那么,如果我不希望Form1中的列表产生影响,该怎么办?

预先感谢您....

尝试在Form2更改代码这样:

Dim lst As List(Of Supplier) 
Dim bs As BindingSource 

Public Sub New(ByVal lst As List(Of Supplier)) 
    Me.InitializeComponent() 
    Me.lst = New List(Of Supplier)(lst) 
    bs = new BindingSource(Me.lst, Nothing) 
End Sub 
+0

是的,它的工作原理。但你能解释一下为什么你这样写:Me.list =新列表(供应商)(1st) – Ericton 2012-08-09 05:19:27

+0

这是写入'lst'(参数)列表副本到'lst'(模块级别)变量,而不仅仅是直接赋值。 – Enigmativity 2012-08-09 05:43:57