// This is pretty big function. It converts the information from a the DataGrid back to the // item table. // Then it matches up the existing items by using the poitem number keyword // Then it adds new items public static void FromGrid(int poid,EMDataSet emDataSet, QuickGrid generalGrid,bool commitItemName) { EMDataSet.POItemTblDataTable itemTable = emDataSet.POItemTbl; // All the new rows are at the end, but with no poitemnumber DataTable grid = (DataTable)generalGrid.GetTable(); // Only work on a temporary copy of the datatable. That way // the one that is bound to the datagrid doesn't change grid = grid.Copy(); // At this point, the entire datatable should be collapsed. // There will be some items with poitemnumbers, and some without int maxSeqNumber = GetMaxSeqNumber(grid); ArrayList poidNumbersInGrid = new ArrayList(); for (int i=0;i<grid.Rows.Count;i++) { DataRow sourceRow = grid.Rows[i]; if (!DataInterface.IsRowAlive(sourceRow)) continue; // Changed row if (!sourceRow.IsNull("POItemNumber")) { int poItemNumber = (int)sourceRow["POItemNumber"]; EMDataSet.POItemTblRow targetRow = itemTable.FindByPOItemNumber(poItemNumber); foreach (DataColumn column in grid.Columns) { string columnName = column.ColumnName; if (columnName == "Finish") { if (commitItemName) TransferFinish("Finish", sourceRow, targetRow); continue; } if (columnName == "Treatment") { if (commitItemName) TransferFinish("Treatment",sourceRow,targetRow); continue; } if (columnName == "ItemName") { if (commitItemName) targetRow["ItemID"] = GetItemID(emDataSet,targetRow,sourceRow["ItemName"]); continue; } targetRow[columnName] = sourceRow[columnName]; } poidNumbersInGrid.Add(poItemNumber); } else // new row { EMDataSet.POItemTblRow targetRow = itemTable.NewPOItemTblRow(); sourceRow["POItemNumber"] = DataInterface.GetNextKeyNumber("tblPOItem2"); sourceRow["SeqNumber"] = (int)(maxSeqNumber+1); maxSeqNumber++; targetRow.POID = poid; foreach (DataColumn column in grid.Columns) { string columnName = column.ColumnName; if (columnName == "Finish") { TransferFinish("Finish",sourceRow,targetRow); continue; } if (columnName == "Treatment") { TransferFinish("Treatment",sourceRow,targetRow); continue; } if (columnName == "ItemName") { targetRow["ItemID"] = GetItemID(emDataSet,targetRow,sourceRow["ItemName"]); continue; } targetRow[columnName] = sourceRow[columnName]; } DataInterface.ConformMetric(itemTable,targetRow); itemTable.AddPOItemTblRow(targetRow); poidNumbersInGrid.Add(sourceRow["POItemNumber"]); } } // Look to see if we need to check any of the items // as being deleted // We have to delete rows // after the iteration so that // the iteration doesn't get screwed // up. This arraylist keeps track of them ArrayList listOfRowsToDelete = new ArrayList(); foreach (EMDataSet.POItemTblRow row in itemTable) { if (!DataInterface.IsRowAlive(row)) continue; bool poidFound = false; int poidNumber = row.POItemNumber; foreach (int gridPOID in poidNumbersInGrid) { if (poidNumber == gridPOID) { poidFound = true; break; } } if (!poidFound) listOfRowsToDelete.Add(row); } foreach (DataRow row in listOfRowsToDelete) row.Delete(); // Test for good seqnumbers ArrayList listOfSeqNumbers = new ArrayList(); foreach (EMDataSet.POItemTblRow row in itemTable) { if (!DataInterface.IsRowAlive(row)) continue; listOfSeqNumbers.Add(row.SeqNumber); } listOfSeqNumbers.Sort(); int [] arrayOfSeqNumbers = (int[])listOfSeqNumbers.ToArray(typeof(int)); for (int i=0;i<listOfSeqNumbers.Count;i++) { if ((i+1) != arrayOfSeqNumbers[i]) throw new Exception("BUG: bad seq number setup"); } }