private static string DataTableDifference(string iProjectName1, ImportedDataTable iImportedDataTable1, string iProjectName2, ImportedDataTable iImportedDataTable2) { object[,] tableData1; object[,] tableData2; //Importation des données des tables try { tableData1 = iImportedDataTable1.GetCachedTableData(); tableData1 = RemoveLastEmptyRowColumnDataTable(iImportedDataTable1); } catch (Exception ex) { throw new Exception("Erreur lors de l'importation des données de la table {0}, projet {1}".FormatString(iImportedDataTable1.DisplayName, iProjectName1), ex); } try { tableData2 = iImportedDataTable2.GetCachedTableData(); tableData2 = RemoveLastEmptyRowColumnDataTable(iImportedDataTable2); } catch (Exception ex) { throw new Exception("Erreur lors de l'importation des données de la table {0}, projet {1}".FormatString(iImportedDataTable1.DisplayName, iProjectName2), ex); } if (tableData1.GetUpperBound(0) != tableData2.GetUpperBound(0)) { return("Le nombre de ligne est différent"); } if (tableData1.GetUpperBound(1) != tableData2.GetUpperBound(1)) { return("Le nombre de colonne est différent"); } //Bouclage sur les lignes for (int rowIndex = 0; rowIndex <= tableData1.GetLength(0) - 1; rowIndex++) { //Bouclage sur les lignes for (int columnIndex = 0; columnIndex <= tableData1.GetLength(1) - 1; columnIndex++) { var data1 = tableData1[rowIndex, columnIndex] != null ? tableData1[rowIndex, columnIndex] : string.Empty; var data2 = tableData2[rowIndex, columnIndex] != null ? tableData2[rowIndex, columnIndex] : string.Empty; if (data1.ToString() != data2.ToString()) { return("La valeur : colonne {0}, ligne {1} est différente.".FormatString(columnIndex + 1, rowIndex + 1)); } } } return(null); }
/// <summary> /// Supprime les éventuelles dernières lignes et colonnes vides /// </summary> /// <param name="iImportedDataTable"></param> /// <returns></returns> private static string[,] RemoveLastEmptyRowColumnDataTable(ImportedDataTable iImportedDataTable) { var rowIndexMax = 0; var columnIndexMax = 0; var tableData = iImportedDataTable.GetCachedTableData(); //Bouclage à l'envers pour trouver les lignes vides for (int rowIndex = tableData.GetLength(0) - 1; rowIndex >= 0; rowIndex--) { //Bouclage sur les celulles de la ligne for (int columnIndex = 0; columnIndex <= tableData.GetLength(1) - 1; columnIndex++) { if (tableData[rowIndex, columnIndex] != null) { if (tableData[rowIndex, columnIndex].ToString() != string.Empty) { rowIndexMax = rowIndex; break; } } } if (rowIndexMax != 0) { break; } } //Bouclage à l'envers pour trouver les colonnes vides for (int columnIndex = tableData.GetLength(1) - 1; columnIndex >= 0; columnIndex--) { //Bouclage sur les celulles de la ligne for (int rowIndex = 0; rowIndex <= tableData.GetLength(0) - 1; rowIndex++) { if (tableData[rowIndex, columnIndex] != null) { if (tableData[rowIndex, columnIndex].ToString() != string.Empty) { columnIndexMax = columnIndex; break; } } } if (columnIndexMax != 0) { break; } } var resultArray = new string[rowIndexMax + 1, columnIndexMax + 1]; //Remplissage du tableau sans les lignes vides ou colonne vides for (int rowIndex = 0; rowIndex <= rowIndexMax; rowIndex++) { for (int columnIndex = 0; columnIndex <= columnIndexMax; columnIndex++) { if (tableData[rowIndex, columnIndex] != null) { resultArray[rowIndex, columnIndex] = tableData[rowIndex, columnIndex].ToString(); } else { resultArray[rowIndex, columnIndex] = null; } } } return(resultArray); }