private void btnAutoMap_Click(object sender, EventArgs e) { var sourceRowsToRemove = new List <DataGridViewRow>(); var destinationRowsToRemove = new List <DataGridViewRow>(); foreach (DataGridViewRow sourceRow in dgvSource.Rows) { if (sourceRow.Index == dgvSource.NewRowIndex) { continue; } string sourceRowValue = sourceRow.Cells[0].Value.ToString(); string sourceFieldType = sourceRow.Cells[1].Value.ToString(); foreach (DataGridViewRow destinationRow in dgvDestination.Rows) { if (destinationRow.Index == dgvDestination.NewRowIndex) { continue; } string destinationFieldType = destinationRow.Cells[1].Value.ToString(); //If Field types do not match if (!sourceFieldType.Equals(destinationFieldType, StringComparison.InvariantCultureIgnoreCase)) { //Then do not map continue; } if (destinationRow.Cells[0].Value.ToString() == sourceRowValue) { var mappedRow = MappingsTable.NewRow(); mappedRow["Source"] = sourceRowValue; mappedRow["Destination"] = sourceRowValue; MappingsTable.Rows.Add(mappedRow); sourceRowsToRemove.Add(sourceRow); destinationRowsToRemove.Add(destinationRow); break; } } } sourceRowsToRemove.ForEach(row => { dgvSource.Rows.Remove(row); }); destinationRowsToRemove.ForEach(row => { dgvDestination.Rows.Remove(row); }); }
private void btnAdd_Click(object sender, EventArgs e) { if (dgvSource.SelectedRows.Count == 0) { MessageBox.Show("Please select a Source field", "Details Required", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (dgvDestination.SelectedRows.Count == 0) { MessageBox.Show("Please select a Destination field", "Details Required", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } var sourceRow = dgvSource.SelectedRows[0]; var destinationRow = dgvDestination.SelectedRows[0]; string sourceFieldType = sourceRow.Cells[1].Value.ToString(); string destinationFieldType = destinationRow.Cells[1].Value.ToString(); if (!sourceFieldType.Equals(destinationFieldType, StringComparison.InvariantCultureIgnoreCase)) { if (MessageBox.Show( "The source field type is different from the destination field type. Are you sure you want to map them?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) { return; } } var row = MappingsTable.NewRow(); row["Source"] = sourceRow.Cells[0].Value.ToString(); row["Destination"] = destinationRow.Cells[0].Value.ToString(); MappingsTable.Rows.Add(row); dgvSource.Rows.Remove(sourceRow); dgvDestination.Rows.Remove(destinationRow); }