DataGridViewにMySQLからデータを取得[VB] 2007年12月1週ソーラー発電
12 月 02

メモとして。

方法としては「名前」と「ふりがな」データを持つクラスをつくり「ToString」では「名前」を返し、比較は「ふりがな」でするようにします。

StringWithClass.vb

Imports System
Imports System.Collections

Public Class StringWithKanaClass
    ' IComparableインターフェイスをインプリメント
    Implements IComparable

    ' メンバ
    Public Name As String
    Public Kana As String

    Public Sub New()
    End Sub

    Public Sub New(ByVal Name As String, ByVal Kana As String)
        MyClass.Name = Name
        MyClass.Kana = Kana
    End Sub

    ' ToString()は「Name」を返す
    Public Overrides Function ToString() As String
        Return Name
    End Function

    '比較する関数
    Public Function CompareTo(ByVal o As Object) As _
                                    Integer Implements IComparable.CompareTo
        '自分と同じ型の場合のみ比較
        If TypeOf o Is StringWithKanaClass Then
            Dim g As StringWithKanaClass = CType(o, StringWithKanaClass)
            Select Case Me.Kana.CompareTo(g.Kana)
                Case 0
                    Return 0
                Case Is > 0
                    Return 1
                Case Is < 0
                    Return -1
            End Select
        End If
        Return 0
    End Function
End Class

DetaGridViewのセルには以下のように代入

DataGridView1(c, i).Value = New StringWithKanaClass("名前","なまえ")
トラックバックURL : http://blog.chibiegg.net/2007/12/02_15_100.htm/trackback

コメントお待ちしております