private void dgvRawList_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0 || e.ColumnIndex < 0) { return; } TreeGridNode curNode = dgvRawList.GetNodeForRow(e.RowIndex); object curValue = dgvRawList[e.ColumnIndex, e.RowIndex].Value; DataBase curRec = curNode.Tag as DataBase; bool isNew = curNode.Tag == null ? true : false; if (curValue == null && isNew) { return; } dgvRawList.CellValueChanged -= new DataGridViewCellEventHandler(dgvRawList_CellValueChanged); // родительский объект DataBase par = curRec == null ? myData : curRec.Parent; if (curRec == null && curNode.Parent != null && curNode.Parent.Index >= 0 && curNode.Parent.Tag != null) { par = curNode.Parent.Tag as DataBase; } // если это последняя строчка, то добавляем следующую пустую, а для этой делаем структуру if (isNew) { curRec = new DataRaw(par); } if (curRec is DataRecept) { DataRecept curRecept = (DataRecept)curRec; // изменение имени if (e.ColumnIndex == dgvRecName.Index && !curRecept.Name.Equals(curValue)) { if (curValue == null || curValue.ToString().Length == 0) { dgvRawList[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Yellow; } else { curRecept.Name = curValue.ToString(); dgvRawList[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.LightGray; } } // изменение выхода if (e.ColumnIndex == dgvRecCountNetto.Index) { try { curRecept.TotalExit = Convert.ToDecimal(curValue, CultureInfo.CurrentCulture); dgvRawList[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.LightGray; } catch (System.Exception) { dgvRawList[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Yellow; } } // обработка компоненты } else { DataRaw curData = (DataRaw)curRec; // изменение потерь if (e.ColumnIndex == dgvRecProcessLoss.Index) { curData.ProcessLoss = Config.DP.GetProcessLossByNum(Convert.ToInt32(curValue, CultureInfo.CurrentCulture)); } decimal brutto = 0; if (curData.RawStruct != null && curData.RawStruct.Brutto != 0) { brutto = curData.RawStruct.Brutto; } // изменение нетто if (e.ColumnIndex == dgvRecCountNetto.Index) { try { curData.Quantity = Convert.ToDecimal(curValue, CultureInfo.CurrentCulture); if (curData.Brutto == 0 && curData.Quantity != 0 && brutto != 0) { dgvRawList[dgvRecCountBrutto.Index, e.RowIndex].Value = CommonFunctions.Round(curData.Quantity * brutto); } dgvRawList[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White; } catch (System.Exception) { dgvRawList[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Yellow; } } // изменение брутто if (e.ColumnIndex == dgvRecCountBrutto.Index) { try { ((DataRaw)curData).Brutto = Convert.ToDecimal(curValue, CultureInfo.CurrentCulture); if (curData.Quantity == 0 && curData.Brutto != 0 && brutto != 0) { dgvRawList[dgvRecCountNetto.Index, e.RowIndex].Value = CommonFunctions.Round(((DataRaw)curData).Brutto / brutto); } dgvRawList[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White; } catch (System.Exception) { dgvRawList[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Yellow; } } // изменение коммента if (e.ColumnIndex == dgvRecComment.Index) { curData.Comment = Convert.ToString(dgvRawList[e.ColumnIndex, e.RowIndex].Value, CultureInfo.CurrentCulture); } } if (isNew) { par.Components.Add(curRec); } dgvRawList.CellValueChanged += new DataGridViewCellEventHandler(dgvRawList_CellValueChanged); }
/// <summary> /// Проверка данных формы и данных рецептуры /// </summary> /// <param name="root">дерево строк формы</param> /// <param name="curRec">рецептура</param> private void CheckReceptData(TreeGridNodeCollection root, DataRecept curRec) { ArrayList toDelete = new ArrayList(); TreeGridNode lastNode = null; // собираем строчки для удаления if (dgvRawList.Nodes.Count > 0) { foreach (TreeGridNode dr in root) { // если это пустая последняя строчка, то пропускаем if (dr.Tag == null) { if (lastNode == null) { lastNode = dr; } else { toDelete.Add(dr); } continue; } DataBase curBase = (DataBase)dr.Tag; if (!curRec.Components.Contains(curBase)) { toDelete.Add(dr); continue; } // если это рецептура, то рекурсия проверки if (curBase is DataRecept) { CheckReceptData(dr.Nodes, curBase as DataRecept); continue; } DataRaw curRaw = curBase as DataRaw; if (curRaw.RawStruct != null) { if (!curRaw.RawStruct.DisplayMember.Equals(dr.Cells[dgvRecName.Index].Value)) { dr.Cells[dgvRecName.Index].Value = curRaw.RawStruct.DisplayMember; } } if (!((Decimal)curRaw.Brutto).Equals(dr.Cells[dgvRecCountBrutto.Index].Value)) { dr.Cells[dgvRecCountBrutto.Index].Value = curRaw.Brutto; } if (curRaw.ProcessLoss != null) { if (!curRaw.ProcessLoss.ValueMember.Equals((dr.Cells[dgvRecProcessLoss.Index] as DataGridViewComboBoxCell).Value)) { dr.Cells[dgvRecProcessLoss.Index].Value = curRaw.ProcessLoss.ValueMember; } } if (!curRaw.Quantity.Equals(dr.Cells[dgvRecCountNetto.Index].Value)) { dr.Cells[dgvRecCountNetto.Index].Value = curRaw.Quantity; } if (!curRaw.Comment.Equals(dr.Cells[dgvRecComment.Index].Value)) { dr.Cells[dgvRecComment.Index].Value = curRaw.Comment; } if (curRaw.RawStruct != null) { if (curRaw.Brutto == 0 && curRaw.Quantity != 0) { dr.Cells[dgvRecCountBrutto.Index].Value = CommonFunctions.Round(curRaw.Quantity * curRaw.RawStruct.Brutto); } if (curRaw.Quantity == 0 && curRaw.Brutto != 0 && curRaw.RawStruct.Brutto != 0) { dr.Cells[dgvRecCountNetto.Index].Value = CommonFunctions.Round(curRaw.Brutto / curRaw.RawStruct.Brutto); } } } } if (toDelete.Count > 0) { foreach (TreeGridNode dr in toDelete) { root.Remove(dr); } } // добавление новых if (curRec.Components.Count > 0) { foreach (DataBase newRec in curRec.Components) { Boolean isExists = false; foreach (TreeGridNode dr in root) { if (newRec.Equals(dr.Tag)) { isExists = true; break; } } if (!isExists) { TreeGridNode node = new TreeGridNode(); node.Tag = newRec; root.Add(node); if (newRec is DataRecept) { //TreeGridNode curNode = root.Add(null, newRec.name, null, null, (newRec as DataRecept).totalExit, newRec.comment); node.Cells[dgvRecName.Index].Value = newRec.Name; node.Cells[dgvRecCountNetto.Index].Value = (newRec as DataRecept).TotalExit; node.DefaultCellStyle.BackColor = Color.LightGray; LoadReceptData(node.Nodes, newRec as DataRecept); continue; } DataRaw curRaw = newRec as DataRaw; //TreeGridNode newNode = root.Add(curRaw.id, null, null, curRaw.brutto, curRaw.quantity, curRaw.comment); node.Cells[dgvRecCountBrutto.Index].Value = curRaw.Brutto; node.Cells[dgvRecCountNetto.Index].Value = curRaw.Quantity; node.Cells[dgvRecComment.Index].Value = curRaw.Comment; if (curRaw.RawStruct != null) { node.Cells[dgvRecName.Index].Value = curRaw.RawStruct.DisplayMember; if (curRaw.Brutto == 0 && curRaw.Quantity != 0 && curRaw.RawStruct.Brutto != 0) { node.Cells[dgvRecCountBrutto.Index].Value = CommonFunctions.Round(curRaw.Quantity * curRaw.RawStruct.Brutto); } if (curRaw.Brutto != 0 && curRaw.Quantity == 0 && curRaw.RawStruct.Brutto != 0) { node.Cells[dgvRecCountNetto.Index].Value = CommonFunctions.Round(curRaw.Brutto / curRaw.RawStruct.Brutto); } } if (curRaw.ProcessLoss != null) { (node.Cells[dgvRecProcessLoss.Index] as DataGridViewComboBoxCell).Value = curRaw.ProcessLoss.ValueMember; } } } } // проверяем, чтобы последняя строчка была последней if (dgvRawList.Nodes.Count > 0 && (lastNode == null || !lastNode.IsLastSibling)) { root.Add(new TreeGridNode()); if (lastNode != null) { root.Remove(lastNode); } } }