0

我有一个子组件,它在 SelectedIndexChange 上 - 客户返回 TransactionBindingSource。我想要在数据绑定控件上显示每个选定客户的最新交易,而不是第一个。奇怪的是,当标签加载时(在“ Enter”事件上,我调用SelectedIndexchange方法并选择了最新记录。但是,当我手动选择另一个客户时,显示每个交易的第一个记录!这就是我有

Private Sub cboCustomerName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCustomerName.SelectedIndexChanged

    resetstats()

    'Try

    Dim iTotalbuyin, iCountBuyins, iSumBuyins, iSumCashouts, iBiggestWin, iBiggestLoss As Integer
    Dim oTotalbuyin, oCountBuyIns, oSumCashouts, oBiggestWin, oBiggestLoss As Object


    '  If Me.cboCustomerName.SelectedIndex <> -1 Then

    Dim drv As DataRowView = CType(Me.cboCustomerName.SelectedItem, DataRowView)

    Dim SelCustId As Integer = drv.Item("CustomerID")

    Me.TransactionTableAdapter.Fill(Me.DbPBDataSet.Transaction, SelCustId)

    Me.TransactionBindingSource.Sort = "TransactionID"

    Me.TransactionBindingSource.MoveLast()

有任何想法吗?

4

1 回答 1

1

您可以制作自己的 SortableBindingList 类,如下所示:

Imports System.ComponentModel
Imports System.Reflection

Public Class SortableBindingList(Of T)
  Inherits BindingList(Of T)
  Private Property IsSorted As Boolean
  Private Property SortDirection As ListSortDirection
  Private Property SortProperty As PropertyDescriptor

  Protected Overrides ReadOnly Property SupportsSortingCore() As Boolean
    Get
      Return True
    End Get
  End Property
  Protected Overrides ReadOnly Property SortDirectionCore() As ListSortDirection
    Get
      Return _SortDirection
    End Get
  End Property
  Protected Overloads Overrides ReadOnly Property SortPropertyCore() As PropertyDescriptor
    Get
      Return _SortProperty
    End Get
  End Property
  Protected Overloads Overrides Sub ApplySortCore(ByVal PDsc As PropertyDescriptor, ByVal Direction As ListSortDirection)
    Dim items As List(Of T) = TryCast(Me.Items, List(Of T))
    If items Is Nothing Then
      IsSorted = False
    Else
      Dim PCom As New PCompare(Of T)(PDsc.Name, Direction)
      items.Sort(PCom)
      IsSorted = True
      SortDirection = Direction
      SortProperty = PDsc
    End If
    OnListChanged(New ListChangedEventArgs(ListChangedType.Reset, -1))
  End Sub
  Protected Overloads Overrides ReadOnly Property IsSortedCore() As Boolean
    Get
      Return _IsSorted
    End Get
  End Property
  Protected Overrides Sub RemoveSortCore()
    _IsSorted = False
  End Sub

#Region " Constructors "
  Sub New(ByVal list As ICollection(Of T))
    MyBase.New(CType(list, Global.System.Collections.Generic.IList(Of T)))
  End Sub

  Sub New()
    MyBase.New()
  End Sub
#End Region

#Region " Property comparer "

  Private Class PCompare(Of T)
    Implements IComparer(Of T)
    Private Property PropInfo As PropertyInfo
    Private Property SortDir As ListSortDirection

    Friend Sub New(ByVal SortProperty As String, ByVal SortDirection As ListSortDirection)
      SortProperty = CheckPropertyName(SortProperty)
      PropInfo = GetType(T).GetProperty(SortProperty)
      SortDir = SortDirection
    End Sub

    ''' <summary>
    ''' Used to change the name of the sorted property for certain classes.
    ''' </summary>
    ''' <param name="SortProperty"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckPropertyName(ByVal SortProperty As String) As String
      If SortProperty = "BinNumber" AndAlso GetType(T).Name = "BinReportDisplay" Then
        Return "BinNumberSort"
      End If
      Return SortProperty
    End Function

    Friend Function Compare(ByVal x As T, ByVal y As T) As Integer Implements IComparer(Of T).Compare
      Return If(SortDir = ListSortDirection.Ascending, Comparer.[Default].Compare(PropInfo.GetValue(x, Nothing),
              PropInfo.GetValue(y, Nothing)), Comparer.[Default].Compare(PropInfo.GetValue(y, Nothing),
                                                                         PropInfo.GetValue(x, Nothing)))
    End Function
  End Class


#End Region

End Class

然后根据您希望的排序方式对列表进行排序。

于 2013-05-23T16:18:43.650 回答