Posts
337
Comments
125
Trackbacks
5
Kleine Funktion SelectDistinct

Hier eine kleine Funktion SelectDistinct für einen Distinct auf eine Spalte eines DataTables. Diese gibt eine DataTable mit einer Spalte zurück, welche die Werte, die mehrfach auftreten nur einmal zurückgibt.

    Public Function SelectDistinct(ByVal TableName As String, ByVal SourceTable As DataTable, ByVal FieldName As String) As DataTable

        Dim dt As New DataTable(TableName)

        dt.Columns.Add(FieldName, SourceTable.Columns(FieldName).DataType)

        Dim LastValue As Object = Nothing

        Dim dr As DataRow

        For Each dr In SourceTable.Select("", FieldName)

            If LastValue Is Nothing Or Not ColumnEqual(LastValue, dr(FieldName)) Then

                LastValue = dr(FieldName)

                If Convert.ToString(LastValue) = "" Then

                    LastValue = Nothing

                End If

                dt.Rows.Add(New Object() {LastValue})

            End If

        Next dr

        If Not (dsTemp Is Nothing) Then

            dsTemp.Tables.Add(dt)

        End If

        Return dt

    End Function

Das Ergebnis kann man anschließend einem DataView zuweisen, mit DataView.Sort sortieren und z.B. in einer DropDowm-Liste ausgeben

posted on Wednesday, April 15, 2009 5:27 AM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 7 and 6 and type the answer here: