Datenvergleichen mit Farblicher Hervorhebung bei differenzen

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

    Datenvergleichen mit Farblicher Hervorhebung bei differenzen

    Hallo zusammen,

    ich habe ein kleines Programm, mit dem ich eine EXCEL Datei in ein DataGidView rein lade und übertrage das an einen SQL Server. (einmalig)

    Jetzt möchte ich diese Daten mit anderen Daten vergleichen und farblich hervorheben bei Änderung im Datagridview, könnt Ihr mir einen Tipp geben wie ich hier am besten vorgehe kann?

    Danke für eure Unterstützung vorab.

    Anbei mein aktueller Code, bin noch am testen:

    Ps. Für Verbesserungsvorschläge wegen meinen Codes, würde ich mich auch freuen. Nur so kann lernen. :D

    VB.NET-Quellcode

    1. Option Strict Off
    2. Imports System.Data.OleDb
    3. Imports System.IO
    4. Imports System.Data.SqlClient
    5. Public Class Form1
    6. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    7. Dim conn As OleDbConnection
    8. Dim dtr As OleDbDataReader
    9. Dim dta As OleDbDataAdapter
    10. Dim cmd As OleDbCommand
    11. Dim dts As DataSet
    12. Dim excel As String
    13. Dim OpenFileDialog As New OpenFileDialog
    14. OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    15. OpenFileDialog.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls"
    16. If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
    17. Dim fi As New FileInfo(OpenFileDialog.FileName)
    18. Dim FileName As String = OpenFileDialog.FileName
    19. excel = fi.FullName
    20. conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties=Excel 12.0;")
    21. dta = New OleDbDataAdapter("Select * From [Sheet1$]", conn)
    22. dts = New DataSet
    23. dta.Fill(dts, "[Sheet1$]")
    24. DataGridView1.DataSource = dts
    25. DataGridView1.DataMember = "[Sheet1$]"
    26. conn.Close()
    27. End If
    28. End Sub
    29. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    30. Dim sqlc As String = "insert into tbl_650 ([Werk],[Auftragsnr.],[Liefernummer],[WarenEm],[Name],[Straßenname],[PLZ Warenempf.],[Ort],[BestNr],[Versandbedingung],[Spalte K],[Liefersperre],[Wunschlieferdatum],[FA Paletten],[Spalte O],[Bruttoauftragsgew.],[Gew-ME]) values(@f1, @f2, @f3, @f4, @f5, @f6, @f7, @f8, @f9, @f10, @f11, @f12, @f13, @f14, @f15, @f16, @f17)"
    31. Dim dp As New SqlDataAdapter
    32. Dim p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17 As SqlParameter
    33. Dim ConString As String = "Data Source=SERVER1;Initial Catalog=Shared;Integrated Security=True;timeout=5"
    34. Using conn As New SqlConnection(ConString)
    35. Try
    36. conn.Open()
    37. dp.InsertCommand = New SqlCommand(sqlc, conn)
    38. p1 = dp.InsertCommand.Parameters.Add("@f1", SqlDbType.NVarChar)
    39. p2 = dp.InsertCommand.Parameters.Add("@f2", SqlDbType.Decimal)
    40. p3 = dp.InsertCommand.Parameters.Add("@f3", SqlDbType.Decimal)
    41. p4 = dp.InsertCommand.Parameters.Add("@f4", SqlDbType.Decimal)
    42. p5 = dp.InsertCommand.Parameters.Add("@f5", SqlDbType.NVarChar)
    43. p6 = dp.InsertCommand.Parameters.Add("@f6", SqlDbType.NVarChar)
    44. p7 = dp.InsertCommand.Parameters.Add("@f7", SqlDbType.NVarChar)
    45. p8 = dp.InsertCommand.Parameters.Add("@f8", SqlDbType.NVarChar)
    46. p9 = dp.InsertCommand.Parameters.Add("@f9", SqlDbType.NVarChar)
    47. p10 = dp.InsertCommand.Parameters.Add("@f10", SqlDbType.NVarChar)
    48. p11 = dp.InsertCommand.Parameters.Add("@f11", SqlDbType.NVarChar)
    49. p12 = dp.InsertCommand.Parameters.Add("@f12", SqlDbType.NVarChar)
    50. p13 = dp.InsertCommand.Parameters.Add("@f13", SqlDbType.DateTime)
    51. p14 = dp.InsertCommand.Parameters.Add("@f14", SqlDbType.Decimal)
    52. p15 = dp.InsertCommand.Parameters.Add("@f15", SqlDbType.Decimal)
    53. p16 = dp.InsertCommand.Parameters.Add("@f16", SqlDbType.Decimal)
    54. p17 = dp.InsertCommand.Parameters.Add("@f17", SqlDbType.NVarChar)
    55. With DataGridView1
    56. For i As Integer = 0 To .Rows.Count - 1
    57. With .Rows(i)
    58. If .Cells(0).Value = Nothing Then
    59. p1.Value = ""
    60. Else
    61. p1.Value = .Cells(0).Value
    62. End If
    63. If .Cells(1).Value = Nothing Then
    64. p2.Value = 0
    65. Else
    66. p2.Value = CDec(.Cells(1).Value)
    67. End If
    68. If .Cells(2).Value = Nothing Then
    69. p3.Value = 0
    70. Else
    71. p3.Value = CDec(.Cells(2).Value)
    72. End If
    73. If .Cells(3).Value = Nothing Then
    74. p4.Value = 0
    75. Else
    76. p4.Value = CDec(.Cells(3).Value)
    77. End If
    78. If .Cells(4).Value = Nothing Then
    79. p5.Value = ""
    80. Else
    81. p5.Value = .Cells(4).Value
    82. End If
    83. If .Cells(5).Value = Nothing Then
    84. p6.Value = ""
    85. Else
    86. p6.Value = .Cells(5).Value
    87. End If
    88. If .Cells(6).Value = Nothing Then
    89. p7.Value = ""
    90. Else
    91. p7.Value = .Cells(6).Value
    92. End If
    93. If .Cells(7).Value = Nothing Then
    94. p8.Value = ""
    95. Else
    96. p8.Value = .Cells(7).Value
    97. End If
    98. If .Cells(8).Value = Nothing Then
    99. p9.Value = ""
    100. Else
    101. p9.Value = .Cells(8).Value
    102. End If
    103. If .Cells(9).Value = Nothing Then
    104. p10.Value = ""
    105. Else
    106. p10.Value = .Cells(9).Value
    107. End If
    108. If .Cells(10).Value = Nothing Then
    109. p11.Value = 0
    110. Else
    111. p11.Value = .Cells(10).Value
    112. End If
    113. If .Cells(11).Value = Nothing Then
    114. p12.Value = ""
    115. Else
    116. p12.Value = .Cells(11).Value
    117. End If
    118. If .Cells(12).Value = Nothing Then
    119. p13.Value = "01.01.1900"
    120. Else
    121. p13.Value = .Cells(12).Value
    122. End If
    123. If .Cells(13).Value = Nothing Then
    124. p14.Value = 0
    125. Else
    126. p14.Value = CDec(.Cells(13).Value)
    127. End If
    128. If .Cells(14).Value = Nothing Then
    129. p15.Value = 0
    130. Else
    131. p15.Value = CDec(.Cells(14).Value)
    132. End If
    133. If .Cells(15).Value = Nothing Then
    134. p16.Value = 0
    135. Else
    136. p16.Value = CDec(.Cells(15).Value)
    137. End If
    138. If .Cells(16).Value = Nothing Then
    139. p17.Value = ""
    140. Else
    141. p17.Value = .Cells(16).Value
    142. End If
    143. Try
    144. dp.InsertCommand.ExecuteNonQuery()
    145. Catch ex As Exception
    146. MsgBox(ex.ToString)
    147. End Try
    148. End With
    149. Next
    150. End With
    151. Catch ex As Exception
    152. MsgBox(ex.ToString)
    153. Finally
    154. conn.Close()
    155. End Try
    156. End Using
    157. End Sub
    158. End Class