public bool Update(GridData grid, GridData.Row row, ref int colIndex, int pathIndex, UpdateParam param) { // ColumnCount maybe change in loop for (; colIndex < grid.ColumnCount; ++colIndex) { ColumnTag tag = grid.GetColumn(colIndex).ColumnTag; switch (tag.Tag) { case ColumnTag.ETag.AddVariable: // end of bean. // 删除Define变化时没有同步的数据。 HashSet <string> removed = new HashSet <string>(); foreach (var k in VariableMap.Keys) { if (tag.PathLast.Define.Parent.GetVariable(k) == null) { removed.Add(k); } } foreach (var k in removed) { DeleteVarData(k); } return(false); } ColumnTag.VarInfo varInfo = tag.Path[pathIndex]; if (false == VariableMap.TryGetValue(varInfo.Define.Name, out var varData)) { switch (param.UpdateType) { case EUpdate.Data: break; // will new data case EUpdate.CallAction: case EUpdate.Grid: if (varInfo.Define.Type == VarDefine.EType.List) { if (tag.Tag == ColumnTag.ETag.ListStart) { ++colIndex; } colIndex = GridData.FindColumnListEnd(grid, colIndex); } continue; // data not found. continue load. case EUpdate.DeleteData: return(true); // data not found. nothing need to delete. default: throw new Exception("unknown update type"); } varData = new VarData(this, varInfo.Define.Name); VariableMap.Add(varInfo.Define.Name, varData); } else if (param.UpdateType == EUpdate.CallAction) { param.UpdateAction(grid, colIndex, varInfo, varData); } if (varInfo.Define.Type == VarDefine.EType.List) { if (param.UpdateType == EUpdate.DeleteData) { if (pathIndex + 1 < tag.Path.Count) { if (varInfo.ListIndex < varData.Beans.Count) { Bean bean1 = varData.Beans[varInfo.ListIndex]; if (null != bean1) { bean1.Update(grid, row, ref colIndex, pathIndex + 1, param); } // always return true; } } else { if (ColumnTag.ETag.ListStart != tag.Tag) { throw new Exception("应该仅在Tag为ListStart时移除数据. see FormMain.deleteVariable..."); } DeleteVarData(varInfo.Define.Name); } return(true); } if (ColumnTag.ETag.ListStart == tag.Tag) { continue; // 此时没有进入下一级Bean,就在当前Bean再次判断,因为这里没有ListIndex。 } if (tag.Tag == ColumnTag.ETag.ListEnd) { int curListCount = -varInfo.ListIndex; int add = 0; for (int i = curListCount; i < varData.Beans.Count; ++i) { ColumnTag tagSeed = tag.Copy(ColumnTag.ETag.Normal); tagSeed.PathLast.ListIndex = i; add += tag.PathLast.Define.Reference.BuildGridColumns(grid, colIndex + add, tagSeed, -1); } if (curListCount < varData.Beans.Count) // curListCount 至少为1. { varInfo.ListIndex = -varData.Beans.Count; } if (add > 0) { --colIndex; // 新增加了列,回退一列,继续装载数据。 } continue; } if (varInfo.ListIndex >= varData.Beans.Count) { if (EUpdate.Data == param.UpdateType) { for (int i = varData.Beans.Count; i < varInfo.ListIndex; ++i) { varData.AddBean(new Bean(Document, varInfo.Define.Value)); } Bean create = new Bean(Document, varInfo.Define.Value); varData.AddBean(create); if (create.Update(grid, row, ref colIndex, pathIndex + 1, param)) { return(true); } } // 忽略剩下的没有数据的item直到ListEnd。 colIndex = GridData.FindColumnListEnd(grid, colIndex); continue; } Bean bean = varData.Beans[varInfo.ListIndex]; if (null != bean) { if (bean.Update(grid, row, ref colIndex, pathIndex + 1, param)) { return(true); } continue; } if (EUpdate.Data == param.UpdateType) { Bean create = new Bean(Document, varInfo.Define.Value); varData.SetBeanAt(varInfo.ListIndex, create); if (create.Update(grid, row, ref colIndex, pathIndex + 1, param)) { return(true); } } continue; } if (pathIndex + 1 != tag.Path.Count) { throw new Exception("Remain Path, But Is Not A List"); } switch (param.UpdateType) { case EUpdate.Data: // OnGridCellEndEdit update data varData.Value = row.Cells[colIndex].Value; return(true); case EUpdate.CallAction: // 上面已经做完了。 break; case EUpdate.Grid: row.Cells[colIndex].Value = varData.Value; // upate to grid break; // Update Grid 等到 ColumnTag.ETag.AddVariable 才返回。在这个函数开头。 case EUpdate.DeleteData: DeleteVarData(varInfo.Define.Name); return(true); default: throw new Exception("unkown update type. end."); } } return(true); }
public void DeleteListItem(int columnIndexSelected) { int colListEnd = GridData.FindColumnListEnd(this, columnIndexSelected); if (colListEnd < 0) { MessageBox.Show("请选择 List 中间的列。"); return; // not in list } ColumnTag tagSelected = Columns[columnIndexSelected].ColumnTag; ColumnTag tagListEnd = Columns[colListEnd].ColumnTag; int pathEndIndex = tagListEnd.Path.Count - 1; int colBeanBegin = GridData.FindColumnBeanBegin(this, columnIndexSelected); int listIndex = tagSelected.Path[pathEndIndex].ListIndex; // delete data(list item) for (int row = 0; row < RowCount; ++row) { Document.Beans[row].GetVarData(0, tagSelected, pathEndIndex)?.DeleteBeanAt(listIndex); } if (tagListEnd.PathLast.ListIndex == -1) { // 只有一个item,仅删除数据,不需要删除Column。需要更新grid。 for (int row = 0; row < RowCount; ++row) { GridData.DoActionUntilBeanEnd(this, colBeanBegin, colListEnd, (int col) => { switch (Columns[col].ColumnTag.Tag) { case ColumnTag.ETag.Normal: var cell = GetCell(col, row); cell.Value = ""; View?.InvalidateCell(col, row); break; } }); } return; } { // delete column List <int> colDelete = new List <int>(); GridData.DoActionUntilBeanEnd(this, colBeanBegin, colListEnd, (int col) => colDelete.Add(col)); for (int i = colDelete.Count - 1; i >= 0; --i) { RemoveColumn(colDelete[i]); } colListEnd -= colDelete.Count; } // reduce ListIndex In Current List after deleted item. while (colBeanBegin < colListEnd) { colBeanBegin = GridData.DoActionUntilBeanEnd(this, colBeanBegin, colListEnd, (int col) => { ColumnTag tagReduce = Columns[col].ColumnTag; --tagReduce.Path[pathEndIndex].ListIndex; }); } ++tagListEnd.PathLast.ListIndex; }