public void BuildGridData() { FormMain.Instance.FormError.RemoveErrorByGrid(GridData); GridData = new GridData(this); BeanDefine.BuildGridColumns(GridData, 0, new ColumnTag(ColumnTag.ETag.Normal), -1); var param = new Bean.UpdateParam() { UpdateType = Bean.EUpdate.Grid }; foreach (var bean in _Beans) { int insertIndex = GridData.RowCount; GridData.InsertRow(insertIndex); int colIndex = 0; if (bean.Update(GridData, GridData.GetRow(insertIndex), ref colIndex, 0, param)) { break; } } for (int i = 0; i < GridData.ColumnCount; ++i) { ColumnTag tag = GridData.GetColumn(i).ColumnTag; switch (tag.Tag) { case ColumnTag.ETag.AddVariable: case ColumnTag.ETag.ListStart: case ColumnTag.ETag.ListEnd: continue; } tag.BuildUniqueIndex(GridData, i); } }
public static int FindColumnBeanBegin(GridData grid, int startColIndex) { int skipNestListCount = 0; for (int c = startColIndex - 1; c >= 0; --c) { ColumnTag tag = grid.GetColumn(c).ColumnTag; if (skipNestListCount > 0) { switch (tag.Tag) { case ColumnTag.ETag.ListEnd: ++skipNestListCount; break; case ColumnTag.ETag.ListStart: --skipNestListCount; break; } continue; } switch (tag.Tag) { case ColumnTag.ETag.AddVariable: case ColumnTag.ETag.ListStart: return(c + 1); //case ColumnTag.ETag.Normal: // break; case ColumnTag.ETag.ListEnd: ++skipNestListCount; break; } } throw new Exception("FindColumnBeanBegin"); }
public static int FindCloseListEnd(GridData grid, int startColIndex) { int listStartCount = 1; for (int c = startColIndex + 1; c < grid.ColumnCount; ++c) { ColumnTag tag = grid.GetColumn(c).ColumnTag; switch (tag.Tag) { case ColumnTag.ETag.ListEnd: --listStartCount; if (listStartCount == 0) { return(c); } break; case ColumnTag.ETag.ListStart: ++listStartCount; break; } } throw new Exception("List Not Closed."); }
public static int FindColumnListEnd(GridData grid, int startColIndex) { int skipNestListCount = 0; for (int c = startColIndex; c < grid.ColumnCount; ++c) { ColumnTag tag = grid.GetColumn(c).ColumnTag; if (skipNestListCount > 0) { switch (tag.Tag) { case ColumnTag.ETag.ListEnd: --skipNestListCount; break; case ColumnTag.ETag.ListStart: ++skipNestListCount; break; } continue; } switch (tag.Tag) { case ColumnTag.ETag.ListStart: ++skipNestListCount; break; //case ColumnTag.ETag.AddVariable: //case ColumnTag.ETag.Normal: // break; case ColumnTag.ETag.ListEnd: return(c); } } return(-1); }
public int BuildGridColumns(GridData grid, int columnIndex, ColumnTag tag, int listIndex) { int colAdded = 0; foreach (var v in _Variables) { colAdded += v.BuildGridColumns(grid, columnIndex + colAdded, tag, listIndex); } // 这里创建的列用来新增。 grid.InsertColumn(columnIndex + colAdded, new GridData.Column() { HeaderText = ",", ReadOnly = true, ToolTipText = "双击增加列", // 使用跟List一样的规则设置ListIndex,仅用于Delete List Item,此时这个Bean肯定在List中。 ColumnTag = tag.Copy(ColumnTag.ETag.AddVariable).AddVar(new VarDefine(this, ""), listIndex >= 0 ? listIndex : 0), }); for (int i = 0; i < grid.RowCount; ++i) { grid.GetCell(columnIndex + colAdded, i).Value = ","; } ++colAdded; return(colAdded); }
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; }
public int BuildGridColumns(GridData grid, int columnIndex, ColumnTag tag, int listIndex) { switch (Type) { case EType.List: { grid.InsertColumn(columnIndex, new GridData.Column() { HeaderText = "[" + this.Name, ReadOnly = true, ToolTipText = Name + ":" + Value + ":" + Comment, ColumnTag = tag.Copy(ColumnTag.ETag.ListStart).AddVar(this, -1), }); for (int i = 0; i < grid.RowCount; ++i) { grid.GetCell(columnIndex, i).Value = "["; } if (null == Reference) { throw new Exception("List Reference Not Initialize."); } ++columnIndex; int colAdded = 0; if (listIndex >= 0) { colAdded = Reference.BuildGridColumns(grid, columnIndex, tag.Copy(tag.Tag).AddVar(this, listIndex), -1); } columnIndex += colAdded; grid.InsertColumn(columnIndex, new GridData.Column() { HeaderText = "]" + this.Name, ReadOnly = true, ToolTipText = Name + ": 双击此列增加List Item。", // 这里的 PathLast.ListIndex 是List中最大的Item数量,以后在Bean.Update中修改。 ColumnTag = tag.Copy(ColumnTag.ETag.ListEnd).AddVar(this, 0), }); for (int i = 0; i < grid.RowCount; ++i) { grid.GetCell(columnIndex, i).Value = "]"; } return(colAdded + 2); } /* * case EType.Enum: * { * DataGridViewCell template = new DataGridViewTextBoxCell(); * ColumnTag current = tag.Copy(tag.Tag).AddVar(this, -1); * grid.Columns.Insert(columnIndex, new DataGridViewColumn(template) * { * Name = this.Name, * Width = GridColumnValueWidth, * ToolTipText = Name + ":" + Comment, * Tag = current, * Frozen = false, * AutoSizeMode = DataGridViewAutoSizeColumnMode.None, * }); * // 自动完成来实现enum选择。不使用Combobox. * current.BuildUniqueIndex(grid, columnIndex); * return 1; * } */ default: { ColumnTag current = tag.Copy(tag.Tag).AddVar(this, -1); grid.InsertColumn(columnIndex, new GridData.Column() { HeaderText = Name, ToolTipText = Name + ":" + Comment, ColumnTag = current, }); current.BuildUniqueIndex(grid, columnIndex); return(1); } } }