在重绘单元格过程中,对于获取排他错误信息有一段如下代码:
- private string GetRowErrors(Column col)
- {
- if (this.Grid == null)
- {
- return null;
- }
- IEditableCollectionView editableCollectionView = this.Grid.CollectionView as IEditableCollectionView;
- if (editableCollectionView != null && this.DataItem == editableCollectionView.CurrentAddItem)
- {
- return null;
- }
- ...
- }
复制代码
由以上代码可知,当该行数据项 this.DataItem 等于 当前编辑视图 IEditableCollectionView 的新增项 CurrentAddItem 时,获取的排他错误为 null,所以导致新增行如果还未换行保存,是不进行排他效果显示的;(实际上,新增行未换行前,属于挂起状态,所以不进行排他验证吧)
两种解决方案:
1、在 C1FlexGrid 的 CellEditEnded 事件中结束事务并保存挂起的新项,
- (flexGrid.CollectionView as IEditableCollectionView).CommitNew();
复制代码
但是这样该行就属于普通行,不属于新增行了;如此的话,只有添加一个变量标记该新增项,作为以后其他的处理了;
2、还有一种解决方案就是把 CellFactory 中的 ApplyCellStyles 方法重新,自己重写 GetErrors 方法,把 if (editableCollectionView != null && this.DataItem == editableCollectionView.CurrentAddItem) 的判断移除,应该就可以达到你要的效果了;
- public override void ApplyCellStyles(C1FlexGrid grid, CellType cellType, CellRange rng, Border bdr)
- {
- base.ApplyCellStyles(grid, cellType, rng, bdr);
- GridPanel panel = grid.Cells;
- Row row = panel.Rows[rng.Row];
- Column column = panel.Columns[rng.Column];
- CellStyle cellStyle = row.CellStyle;
- CellStyle cellStyle2 = column.CellStyle;
- if (panel.CellType == CellType.Cell && grid.ShowErrors && grid.ErrorStyle != null)
- {
- string errors = GetErrors(row, column);
- if (!string.IsNullOrEmpty(errors))
- {
- grid.ErrorStyle.Apply(bdr);
- this.SetErrorTip(grid, bdr, errors);
- }
- }
- }
- private string GetErrors(Row row, Column col)
- {
- string text = (col != null) ? col.BoundPropertyName : null;
- DataRowView dataRowView = row.DataItem as DataRowView;
- if (dataRowView != null)
- {
- if (!dataRowView.Row.HasErrors)
- {
- return null;
- }
- if (col != null)
- {
- return dataRowView.Row.GetColumnError(col.ColumnName);
- }
- else
- {
- List<string> list = new List<string>();
- if (!string.IsNullOrEmpty(dataRowView.Row.RowError))
- {
- list.Add(dataRowView.Row.RowError);
- }
- foreach (Column current in row.Grid.Columns)
- {
- string columnError;
- columnError = dataRowView.Row.GetColumnError(current.ColumnName);
- if (!string.IsNullOrEmpty(columnError) && !list.Contains(columnError))
- {
- list.Add(columnError);
- }
- }
- if (list.Count <= 0)
- {
- return null;
- }
- return string.Join("\r\n", list.ToArray());
- }
- }
- else
- {
- IDataErrorInfo dataErrorInfo = row.DataItem as IDataErrorInfo;
- if (dataErrorInfo == null)
- {
- return null;
- }
- if (!string.IsNullOrEmpty(text))
- {
- return dataErrorInfo[text];
- }
- List<string> list2 = new List<string>();
- if (!string.IsNullOrEmpty(dataErrorInfo.Error))
- {
- list2.Add(dataErrorInfo.Error);
- }
- foreach (Column current2 in row.Grid.Columns)
- {
- if (!string.IsNullOrEmpty(current2.BoundPropertyName))
- {
- string text2 = dataErrorInfo[current2.BoundPropertyName];
- if (!string.IsNullOrEmpty(text2) && !list2.Contains(text2))
- {
- list2.Add(text2);
- }
- }
- }
- if (list2.Count <= 0)
- {
- return null;
- }
- return string.Join("\r\n", list2.ToArray());
- }
- }
复制代码 |