Home > Blockchain >  How can i convert a datatable into an object in vb.net?
How can i convert a datatable into an object in vb.net?

Time:09-21

I have two DataTables (dt1 and dt2) and i want to convert them into an object of a class (data1 and data2), so i can use the compareTo method an put it into a binding List.

Public Class MainForm

    Public dt1, dt2 As DataTable
    Public data1, data2 As ISAACService
    Private Sub btnDatei1_Click(sender As Object, e As EventArgs) Handles btnDatei1.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            If File.Exists(OpenFileDialog1.FileName) Then
                dt1 = FileGenerator.ReadFromProtectedFile(OpenFileDialog1.FileName)
                dgv1.DataSource = dt1
            End If
        End If
    End Sub

    Private Sub btnVergleich_Click(sender As Object, e As EventArgs) Handles btnVergleich.Click
        CompareDataTables()
    End Sub




    Private Sub btnDatei2_Click(sender As Object, e As EventArgs) Handles btnDatei2.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            dt2 = FileGenerator.ReadFromProtectedFile(OpenFileDialog1.FileName)
            dgv2.DataSource = dt2
        End If
    End Sub

    Private Sub CompareDataTables()
        data1 = CType(dt1, ISAACService)
        For i = 0 To dt1.Rows.Count - 1
            If i < dt2.Rows.Count Then
                Dim row1 = dt1.Rows(i)
                Dim row2 = dt2.Rows(i)
                data1.CompareTo(data2)
                '
            End If
        Next
    End Sub
End Class

The compareTo method:

Public Function CompareTo(other As ISAACService) As Integer Implements IComparable(Of ISAACService).CompareTo

        If other.GetType() Is GetType(ISAACService) Then
            other = CType(other, ISAACService)

            If Me.UANR > other.UANR Then
                Return 1
            ElseIf Me.UANR < other.UANR Then
                Return -1
            Else
                Return 0
            End If
        End If
        Return 0

    End Function

The class for the object:

Public MustInherit Class ISAACServiceBase


    ReadOnly Property KostenArt As String
    ReadOnly Property UANR As String
    ReadOnly Property Überbegriff As String
    ReadOnly Property Benennung As String
    ReadOnly Property Anzahl As Double
    ReadOnly Property Einheit As String
    ReadOnly Property Einzelkosten As Double
    ReadOnly Property Gesamtmenge As Integer
    ReadOnly Property Z As String

    Public Sub New()

    End Sub

    Public Sub New(kArt As String, uNR As String, üBegriff As String, bnung As String, anzl As Double, enht As String, eKosten As Double, gMenge As Integer, zz As String)
        KostenArt = kArt
        UANR = uNR
        Überbegriff = üBegriff
        Benennung = bnung
        Anzahl = anzl
        Einheit = enht
        Einzelkosten = eKosten
        Gesamtmenge = gMenge
        Z = zz
    End Sub

End Class

Please let me know if you need any further information. Thank you for helping out!

CodePudding user response:

Here is a function to do the conversions. Of course, I have no idea if the DataTable fields line up with the Properties of the class. You may have to change the field indexes for the row.

Private Function CovertDataTableToListOfISAACService(dt As DataTable) As List(Of ISAACService)
    Dim lst As New List(Of ISAACService)
    For Each row As DataRow In dt.Rows
        Dim ISAAC As New ISAACService(row(0).ToString, row(1).ToString, row(2).ToString, row(3).ToString, CDbl(row(4)), row(5).ToString, CDbl(row(6)), CInt(row(7)), row(8).ToString)
        lst.Add(ISAAC)
    Next
    Return lst
End Function

Your compare function starts out validating the type of other. other is already guaranteed to be ISAACService because that is the type of the parameter. I am not sure what you are comparing other to but that is not what the question asks.

  • Related