【解決方法】VB.NET でデータテーブル列を 3 つの値でカスタムソートする方法


VBA コードを VB.net に移行しています。 従業員、配偶者、子供の 3 つのテキスト値に基づいてカスタムの並べ替えを作成する必要があるという問題に遭遇しました。

カスタムソートに関する記事はいくつかありますが、それらはすべて数値または日付を中心に展開しています。 ‘

これが私がこれまでに持っているものです:

VB.NET
Sub Sort_information()

    Dim sort_str As String
    Dim dv As DataView
    Dim col = 2 'Or name

    dv = individual.dt.DefaultView


    Dim anyHasValue = Demographic_data_view.Rows.
        OfType(Of DataGridViewRow).
        Where(Function(r) Not r.IsNewRow AndAlso r.Cells.
            OfType(Of DataGridViewTextBoxCell).
            Any(Function(c) c.OwningColumn.Index = col AndAlso
                If(c.Value, String.Empty).ToString().Trim().Length > 0)).
        Any()

    'this line of code allows you to sort a range of columns that contain the data
    If anyHasValue = True Then
        sort_str = "Employee_SSN ASC, "
    Else
        'Sort by individual ID column
        sort_str = "Individual_ID ASC, "
    End If

    'dv = New DataView(individual.dt,, "type Asc", DataViewRowState.CurrentRows)

    '.SortFields.Add Key:=Range("H10:H" & vRowc), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:="Employee,Spouse,Child,"
    '.SortFields.Add Key:=Range("D10:D" & vRowc), SortOn:=xlSortOnValues, Order:=xlAscending
    '.SortFields.Add Key:=Range("E10:E" & vRowc), SortOn:=xlSortOnValues, Order:=xlAscending
    '.SortFields.Add Key:=Range("W10:W" & vRowc), SortOn:=xlSortOnValues, Order:=xlAscending

    'the excel sortfilds will be converted over to vb.net as text values incorportated into the sort_str variable

    dv.Sort = sort_str
    Demographic_data_view.DataSource = dv
End Sub

目標は、SSN または ID (SSN が提供されているかどうかによって異なります)、「Employee,Spouse,Child,」、first_name、Last_name、および Plan_name に基づいて情報をソートすることです。

カスタムソートを除外すると、文字列変数を簡単に作成して、他の列のソートを行うことができます。 ただし、従業員、配偶者、子の順番は絶対に必要です。

この問題について何か助けていただければ幸いです。

私が試したこと:

3 つのテキスト値でのカスタム並べ替えに特化した記事を見つけようとしました。 しかし、私は何も思いつきませんでした。

解決策 1

コメント

タイトルとURLをコピーしました