raocarlos600
3/6/2018 - 1:41 PM

Recorrido DataTables

Recorre dos DataTables comparando contenido y guardando ambos en un DataGridView en la misma fila. Es continuación del snippet "Excel to DataTable"

If dtOdin.Rows.Count <> dtCreado.Rows.Count Then
    MsgBox("¡Los excel tienen distinto número de filas!")
End If

Dim encontrado As Boolean
For Each rowOdin As DataRow In dtOdin.Rows
    encontrado = False
    For Each rowCreado As DataRow In dtCreado.Rows
        If IIf(rowCreado(0) Is DBNull.Value, "", Convert.ToString(rowCreado(0))) = IIf(rowOdin(0) Is DBNull.Value, "", Convert.ToString(rowOdin(0))) AndAlso IIf(rowCreado(1) Is DBNull.Value, "", Convert.ToString(rowCreado(1))) = IIf(rowOdin(1) Is DBNull.Value, "", Convert.ToString(rowOdin(1))) AndAlso IIf(rowCreado(2) Is DBNull.Value, "", Convert.ToString(rowCreado(2))) = IIf(rowOdin(2) Is DBNull.Value, "", Convert.ToString(rowOdin(2))) AndAlso IIf(rowCreado(3) Is DBNull.Value, "", Convert.ToString(rowCreado(3))) = IIf(rowOdin(3) Is DBNull.Value, "", Convert.ToString(rowOdin(3))) AndAlso IIf(rowCreado(5) Is DBNull.Value, "", Convert.ToString(rowCreado(5))) = IIf(rowOdin(5) Is DBNull.Value, "", Convert.ToString(rowOdin(5))) Then
            'poner en data grid los datos
            Dim row As String() = New String() {IIf(rowOdin(0) Is DBNull.Value, "", rowOdin(0)), IIf(rowOdin(1) Is DBNull.Value, "", rowOdin(1)), IIf(rowOdin(2) Is DBNull.Value, "", rowOdin(2)), IIf(rowOdin(3) Is DBNull.Value, "", rowOdin(3)), IIf(rowOdin(4) Is DBNull.Value, "", rowOdin(4)), IIf(rowOdin(5) Is DBNull.Value, "", rowOdin(5)), IIf(rowOdin(6) Is DBNull.Value, "", rowOdin(6)), IIf(rowCreado(0) Is DBNull.Value, "", rowCreado(0)), IIf(rowCreado(1) Is DBNull.Value, "", rowCreado(1)), IIf(rowCreado(2) Is DBNull.Value, "", rowCreado(2)), IIf(rowCreado(3) Is DBNull.Value, "", rowCreado(3)), IIf(rowCreado(4) Is DBNull.Value, "", rowCreado(4)), IIf(rowCreado(5) Is DBNull.Value, "", rowCreado(5)), IIf(rowCreado(6) Is DBNull.Value, "", rowCreado(6))}
            DataGridView1.Rows.Add(row)
            encontrado = True
            Exit For
        End If
    Next
    If encontrado = False Then
        Dim row As String() = New String() {IIf(rowOdin(0) Is DBNull.Value, "", rowOdin(0)), IIf(rowOdin(1) Is DBNull.Value, "", rowOdin(1)), IIf(rowOdin(2) Is DBNull.Value, "", rowOdin(2)), IIf(rowOdin(3) Is DBNull.Value, "", rowOdin(3)), IIf(rowOdin(4) Is DBNull.Value, "", rowOdin(4)), IIf(rowOdin(5) Is DBNull.Value, "", rowOdin(5)), IIf(rowOdin(6) Is DBNull.Value, "", rowOdin(6)), "", "", "", "", "", "", ""}
        DataGridView1.Rows.Add(row)
    End If
Next