2013-05-23 7 views
0

고객의 SelectedIndexChange -에있는 TransactionBindingSource를 반환하는 하위 항목이 있습니다. 내가 원하는 것은 각 고객의 첫 번째 트랜잭션 대신 최신 트랜잭션을 데이터 바인딩 컨트롤에 표시하는 것입니다. 이상한 것은 탭이로드 될 때 ('enter'이벤트에서 selectedIndexChange 메소드를 호출하고 최신 레코드가 선택된다는 것입니다. 그러나 수동으로 다른 고객을 선택하면 각 트랜잭션의 첫 번째 레코드가 표시됩니다!SelectedIndexChange에서 BindingSource를 처음으로 정렬 할 수 없음

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() 

어떤 아이디어가

답변

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 

그런 다음 당신이 그것을 정렬 할 방법에 따라 목록을 정렬

관련 문제