Vb.net Can';从第一次对SelectedIndexChange排序BindingSource

Vb.net Can';从第一次对SelectedIndexChange排序BindingSource,vb.net,vb.net-2010,Vb.net,Vb.net 2010,我有一个sub,它在SelectedIndexChange-of Customer上返回TransactionBindingSource。 我想在数据绑定控件上显示每个选定客户的最新交易,而不是第一个交易。奇怪的是,当选项卡加载(在“enter”事件中)时,我调用selectedIndexChange方法并选择最新记录。然而,当我手动选择另一个客户时,会显示每笔交易的第一条记录!这就是我拥有的 Private Sub cboCustomerName_SelectedIndexChanged(se

我有一个sub,它在SelectedIndexChange-of Customer上返回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()
私有子cboCustomerName\u SelectedIndexChanged(发送方作为对象,e作为事件参数)处理cboCustomerName.SelectedIndexChanged
resetstats()
“试试看
Dim iTotalbuyin、iCountBuyin、iSumbuyin、iSumCashouts、iBigGetWin、iBigGetLoss作为整数
Dim oTotalbuyin、OcountBuyin、Osumcahouts、oBiggestWin、oBiggestLoss作为对象
'如果Me.cboCustomerName.SelectedIndex-1,则
作为DataRowView=CType的Dim drv(Me.cboCustomerName.SelectedItem,DataRowView)
Dim SelCustId为整数=drv.Item(“CustomerID”)
Me.TransactionTableAdapter.Fill(Me.DbPBDataSet.Transaction,SelCustId)
Me.TransactionBindingSource.Sort=“TransactionID”
Me.TransactionBindingSource.MoveLast()

有什么想法吗?

您可以创建自己的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
导入System.ComponentModel
输入系统。反射
公共类SortableBindingList(共T个)
继承绑定列表(共T个)
私有属性被分类为布尔值
私有属性SortDirection作为ListSortDirection
私有属性SortProperty作为属性描述符
受保护的重写只读属性SupportsOrtingCore()为布尔值
得到
返回真值
结束
端属性
受保护的重写只读属性SortDirectionCore()为ListSortDirection
得到
返回方向
结束
端属性
受保护的重载将只读属性SortPropertyCore()作为PropertyDescriptor重写
得到
返回\u排序属性
结束
端属性
受保护的重载覆盖子ApplySortCore(ByVal PDsc作为PropertyDescriptor,ByVal方向作为ListSortDirection)
作为列表(共T个)的Dim项目=TryCast(共T个项目,共T个列表))
如果项目不算什么,那么
IsSorted=False
其他的
Dim PCom作为新的PCompare(共T个)(PDsc.名称、方向)
物料分类(PCom)
IsSorted=True
排序方向=方向
SortProperty=PDsc
如果结束
OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1))
端接头
受保护的重载将只读属性IsSortedCore()重写为布尔值
得到
返回已排序
结束
端属性
受保护的覆盖子RemoveSortCore()
_IsSorted=False
端接头
#地区“建设者”
新建子目录(ByVal列表作为ICollection(Of T))
新建(CType(list,Global.System.Collections.Generic.IList(Of T)))
端接头
次新
MyBase.New()
端接头
#末端区域
#区域“属性比较器”
私人类PCompare(Of T)
执行IComparer(属于T)
私有财产信息作为财产信息
私有财产SortDir作为列表SORTDIRECTION
Friend Sub New(ByVal SortProperty作为字符串,ByVal SortDirection作为列表SortDirection)
SortProperty=CheckPropertyName(SortProperty)
PropInfo=GetType(T).GetProperty(SortProperty)
SortDir=SortDirection
端接头
''' 
''用于更改某些类的排序属性的名称。
''' 
''' 
''' 
''' 
私有函数CheckPropertyName(ByVal SortProperty作为字符串)作为字符串
如果SortProperty=“BinNumber”和GetType(T).Name=“BinReportDisplay”,则
返回“BinNumberSort”
如果结束
返回排序属性
端函数
友元函数Compare(ByVal x作为T,ByVal y作为T)作为整数实现IComparer(Of T)。Compare
返回If(SortDir=ListSortDirection.Ascending,Comparer.[Default].Compare(PropInfo.GetValue(x,Nothing)),
PropInfo.GetValue(y,Nothing)),比较器。[默认值]。比较(PropInfo.GetValue(y,Nothing),
PropInfo.GetValue(x,Nothing)))
端函数
末级
#末端区域
末级
然后根据您希望的排序方式对列表进行排序