locked
Two Controls Sharing Same Data Source Moves Both List Positions RRS feed

  • Question

  • Hi All,

    I have a list box bound to a data source (object source using BindingList<>) and a combo in a grid control bound to the same source.  When I select items from the combo in the grid, the currently selected item in the listbox changes too.  This is kinda wierd, is there any way to stop this?

    Thanks!
    Rael
    Saturday, April 7, 2007 2:57 AM

Answers

  • Hi,I use different bindingcontex to each control,code like this :

    Imports System

    Imports System.Collections.Generic

    Imports System.ComponentModel

    Imports System.Drawing

    Imports System.Text

    Imports System.Windows.Forms

    Public Class Form1

        Private WithEvents listOfParts As BindingList(Of Part)

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

            InitializeListOfParts()

            Dim bc As New BindingContext

            Dim bc2 As New BindingContext

            Me.ListBox1.BindingContext = bc

            Me.ComboBox1.BindingContext = bc2

            ListBox1.DataSource = listOfParts

            ListBox1.DisplayMember = "PartName"

            Me.ComboBox1.DisplayMember = "PartName"

            Me.ComboBox1.ValueMember = "PartNumber"

            Me.ComboBox1.DataSource = listOfParts

        End Sub

     

        Private Sub InitializeListOfParts()

            listOfParts = New BindingList(Of Part)

            listOfParts.AllowNew = True

            listOfParts.AllowRemove = False

            listOfParts.RaiseListChangedEvents = True

            listOfParts.AllowEdit = False

            listOfParts.Add(New Part("Widget", 1234))

            listOfParts.Add(New Part("Gadget", 5647))

            listOfParts.Add(New Part("Gadge", 5648))

        End Sub

    End Class

    Public Class Part

        Private name As String

        Private number As Integer

     

        Public Sub New()

        End Sub

     

        Public Sub New(ByVal nameForPart As String, _

            ByVal numberForPart As Integer)

            PartName = nameForPart

            PartNumber = numberForPart

     

        End Sub

     

     

        Public Property PartName() As String

            Get

                Return name

            End Get

            Set(ByVal value As String)

                name = Value

            End Set

        End Property

     

        Public Property PartNumber() As Integer

            Get

                Return number

            End Get

            Set(ByVal value As Integer)

                number = Value

            End Set

        End Property

    End Class

     

     

    Hope it helps.

    Regards.

    Monday, April 9, 2007 3:01 AM

All replies

  • The forms currency manager will keep them in sync.  I would create 2 bindingsources from the same bindinglist and bind the listbox and the combobox to different bindingsources.
    Saturday, April 7, 2007 10:53 PM
  • I ran into this similar problem a little bit ago.  My solution was to return a new List<> instead of just a copy.  Seemed to fix it for my situation.
    Sunday, April 8, 2007 5:14 AM
  • Hi,I use different bindingcontex to each control,code like this :

    Imports System

    Imports System.Collections.Generic

    Imports System.ComponentModel

    Imports System.Drawing

    Imports System.Text

    Imports System.Windows.Forms

    Public Class Form1

        Private WithEvents listOfParts As BindingList(Of Part)

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

            InitializeListOfParts()

            Dim bc As New BindingContext

            Dim bc2 As New BindingContext

            Me.ListBox1.BindingContext = bc

            Me.ComboBox1.BindingContext = bc2

            ListBox1.DataSource = listOfParts

            ListBox1.DisplayMember = "PartName"

            Me.ComboBox1.DisplayMember = "PartName"

            Me.ComboBox1.ValueMember = "PartNumber"

            Me.ComboBox1.DataSource = listOfParts

        End Sub

     

        Private Sub InitializeListOfParts()

            listOfParts = New BindingList(Of Part)

            listOfParts.AllowNew = True

            listOfParts.AllowRemove = False

            listOfParts.RaiseListChangedEvents = True

            listOfParts.AllowEdit = False

            listOfParts.Add(New Part("Widget", 1234))

            listOfParts.Add(New Part("Gadget", 5647))

            listOfParts.Add(New Part("Gadge", 5648))

        End Sub

    End Class

    Public Class Part

        Private name As String

        Private number As Integer

     

        Public Sub New()

        End Sub

     

        Public Sub New(ByVal nameForPart As String, _

            ByVal numberForPart As Integer)

            PartName = nameForPart

            PartNumber = numberForPart

     

        End Sub

     

     

        Public Property PartName() As String

            Get

                Return name

            End Get

            Set(ByVal value As String)

                name = Value

            End Set

        End Property

     

        Public Property PartNumber() As Integer

            Get

                Return number

            End Get

            Set(ByVal value As Integer)

                number = Value

            End Set

        End Property

    End Class

     

     

    Hope it helps.

    Regards.

    Monday, April 9, 2007 3:01 AM
  • Interesting.  I'll try this out!

    Thanks guys!!
    Monday, April 9, 2007 6:02 AM