开发者

ListBox control changes selection when DataGridView Combo Box selection is changed

开发者 https://www.devze.com 2023-04-11 05:38 出处:网络
Ok, this behavior seems very strange. I have a ListBox which is bound to an IBindingList of strings. In a DataGridView next to it, i have added a combo box column that is bound to the same List, with

Ok, this behavior seems very strange. I have a ListBox which is bound to an IBindingList of strings. In a DataGridView next to it, i have added a combo box column that is bound to the same List, with this code in the DataBindingComplete event.

            gridPallets.Columns.Remove("Truck")

            Dim Col = gridPallets.Columns.Add(New Windows.Forms.DataGridViewComboBoxColumn())
            Dim CBC As Windows.Forms.DataGridViewComboBoxColumn = gridPallets.Columns(Col)
            CBC.HeaderText = "Truck"
            CBC.Name = "Truck"
            CBC.DataPropertyName = "Truck"
            CBC.DataSource = PackingList.Trucks

The ListBox Datasource is set to the same list object when the form loads.

            lstTrucks.DataSource = PackingList.Trucks

The strange part is that everytime i change the combo box selection in any row of the grid, the selected item in the list box changes to match. The reverse behavior doesn't occur. I haven't done anything in code to purposely facilitate that.

What could cause this? This doesn't make any sense to me.

Update, Stack trace from lstTrucks.SelectedValueChanged event,

>   CKProject.dll!CKProject.UI.cntPackingList.lstTrucks_SelectedValueChanged(Object sender = {SelectedItem = "Truck 2"}, System.EventArgs e = {System.EventArgs}) Line 221  Basic
    System.Windows.Forms.dll!System.Windows.Forms.ListControl.OnSelectedValueChanged(System.EventArgs e) + 0x68 bytes   
    System.Windows.Forms.dll!System.Windows.Forms.ListBox.OnSelectedValueChanged(System.EventArgs e) + 0x11 bytes   
    System.Windows.Forms.dll!System.Windows.Forms.ListBox.OnSelectedIndexChanged(System.EventArgs e) + 0x19 bytes   
    System.Windows.Forms.dll!System.Windows.Forms.ListBox.SelectedIndex.set(int value) + 0x155 bytes    
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnPositionChanged(System.EventArgs e) + 0x49 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.ChangeRecordState(int newPosition, bool validating, bool endCurrentEdit, bool firePositionChange, bool pullData) + 0x180 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.Position.set(int value) + 0x5a bytes  
    System.Windows.Forms.dll!System.Windows.Forms.DataGridViewComboBoxEditingControl.OnSelectedIndexChanged(System.EventArgs e) + 0x11 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.ComboBox.WndProc(ref System.Windows.Forms.Message m) + 0x3d7 bytes    

This is the code for the IBindingList:

<Serializable()>
Public Class GenericBindingList(Of T)
    Inherits List(Of T)

    Implements System.ComponentModel.IBindingList
    Implements ITypedList
    Implements ICloneable



    Const _AllowNew As Boolean = True
    Const _AllowEdit As Boolean = True
    Const _AllowRemove As Boolean = True
    Const _SupportsSorting As Boolean = False
    Const _SupportsChangeNotification As Boolean = True
    Const _SupportsSearching As Boolean = True

    Shadows Sub Add(Item As T)
        MyBase.Add(Item)
        RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.ItemAdded, IndexOf(Item)))
    End Sub

    Shadows Sub Remove(item As T)
        Dim DeleteIndex As Integer = IndexOf(item)
        MyBase.Remove(item)
        RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.ItemDeleted, DeleteIndex))
    End Sub

    <NonSerialized()> _
    Private properties As PropertyDescriptorCollection

    Private m_Indexes As New Hashtable

    Public Sub AddIndex(ByVal [property] As System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.AddIndex
        If Not m_Indexes.ContainsKey([property]) Then
            m_Indexes.Add([property], New Hashtable)
        End If
    End Sub
    Public Sub RemoveIndex(ByVal [property] As System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.RemoveIndex
        m_Indexes.Remove([property])
    End Sub

    Public Overridable Function AddNew() As Object Implements System.ComponentModel.IBindingList.AddNew
        Dim item As T = Activator.CreateInstance(Of T)()
        Add(item)
        Dim index As Integer = IndexOf(item)




        RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.ItemAdded, index))
        Return item
    End Function

    Public ReadOnly Property AllowEdit() As Boolean Implements System.ComponentModel.IBindingList.AllowEdit
        Get
            Return _AllowEdit
        End Get
    End Property

    Public ReadOnly Property AllowNew() As Boolean Implements System.ComponentModel.IBindingList.AllowNew
        Get
            Return _AllowNew
        End Get
    End Property

    Public ReadOnly Property AllowRemove() As Boolean Implements System.ComponentModel.IBindingList.AllowRemove
        Get
            Return _AllowRemove
        End Get
    End Property

    Public Sub ApplySort(ByVal [property] As System.ComponentModel.PropertyDescriptor, ByVal direction As System.ComponentModel.ListSortDirection) Implements System.ComponentModel.IBindingList.ApplySort
        If SupportsSorting Then
            Throw New NotImplementedException
            RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.Reset, 0))
        Else
            Throw New NotSupportedExcept开发者_开发技巧ion
        End If
    End Sub

    Public Function IBindingListFind(ByVal [property] As System.ComponentModel.PropertyDescriptor, ByVal key As Object) As Integer Implements System.ComponentModel.IBindingList.Find
        If m_Indexes.Contains([property]) Then
            If CType(m_Indexes([property]), Hashtable).Contains(key) Then
                Return CType(CType(m_Indexes([property]), Hashtable)(key), Integer)
            End If
        End If
        Return -1
    End Function

    Public ReadOnly Property IsSorted() As Boolean Implements System.ComponentModel.IBindingList.IsSorted
        Get
            If SupportsSorting Then
                Throw New NotImplementedException
            Else
                Return False
            End If
        End Get
    End Property

    Public Event ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Implements System.ComponentModel.IBindingList.ListChanged

    Public Sub RaiseItemChanged(ByVal value As Object)
        If value Is Nothing Then Return
        Dim index As Integer = Me.IndexOf(value)
        If index >= 0 Then
            RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.ItemChanged, index))
        End If
    End Sub

    Public Sub RemoveSort() Implements System.ComponentModel.IBindingList.RemoveSort
        If SupportsSorting Then
            Throw New NotImplementedException
            RaiseEvent ListChanged(Me, New ListChangedEventArgs(ListChangedType.Reset, 0))
        Else
            Throw New NotSupportedException
        End If
    End Sub

    Public ReadOnly Property SortDirection() As System.ComponentModel.ListSortDirection Implements System.ComponentModel.IBindingList.SortDirection
        Get
            If SupportsSorting Then
                Throw New NotImplementedException
            Else
                Return ListSortDirection.Ascending
            End If
        End Get
    End Property

    Public ReadOnly Property SortProperty() As System.ComponentModel.PropertyDescriptor Implements System.ComponentModel.IBindingList.SortProperty
        Get
            If SupportsSorting Then
                Throw New NotImplementedException
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public ReadOnly Property SupportsChangeNotification() As Boolean Implements System.ComponentModel.IBindingList.SupportsChangeNotification
        Get
            Return _SupportsChangeNotification
        End Get
    End Property

    Public ReadOnly Property SupportsSearching() As Boolean Implements System.ComponentModel.IBindingList.SupportsSearching
        Get
            Return _SupportsSearching
        End Get
    End Property

    Public ReadOnly Property SupportsSorting() As Boolean Implements System.ComponentModel.IBindingList.SupportsSorting
        Get
            Return _SupportsSorting
        End Get
    End Property

    Public Function GetItemProperties(ByVal listAccessors() As System.ComponentModel.PropertyDescriptor) As System.ComponentModel.PropertyDescriptorCollection Implements System.ComponentModel.ITypedList.GetItemProperties

        Dim pdc As PropertyDescriptorCollection = Nothing

        If listAccessors Is Nothing OrElse listAccessors.Count = 0 Then
            ' Return properties in sort order
            If properties Is Nothing Then
                properties = TypeDescriptor.GetProperties(GetType(T), New Attribute() {New BrowsableAttribute(True)})
            End If
            pdc = properties
        Else
            ' Return child list shape
            pdc = Windows.Forms.ListBindingHelper.GetListItemProperties(listAccessors(0).PropertyType)
        End If
        Return pdc

    End Function

    Public Function GetListName(listAccessors() As System.ComponentModel.PropertyDescriptor) As String Implements System.ComponentModel.ITypedList.GetListName

        Return GetType(T).Name

    End Function


    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim out As New GenericBindingList(Of T)
        Me.ForEach(Sub(x) out.Add(DataInterface.GenericClone(x)))
        Return out
    End Function
End Class

PackingList.Trucks is GenericBindingList(Of String)

0

精彩评论

暂无评论...
验证码 换一张
取 消