【LeanEAP.NET】精益企业应用平台实战----表格批量编辑与Undo/Redo功能实现
2012-06-01 23:56
906 查看
利用EAP.Entity提供的可以Undo/Redo的列表绑定数据到DataGridView,让表格批量编辑更方便更高效。
1. 还是利用AccountGroup表的结构,然后得到实体AccountGroupModel:
View Code
7. 运行结果
示例代码下载EntityDemo.zip
【LeanEAP.NET】精益企业应用平台----系列目录
1. 还是利用AccountGroup表的结构,然后得到实体AccountGroupModel:
View Code
public partial class DemoForm : KryptonForm { //业务接口 IAccountGroupManager manager; //实体列表 DataObjectList<AccountGroupModel> models = new DataObjectList<AccountGroupModel>(); public DemoForm() { InitializeComponent(); //创建接口的实现 manager = new RefObjectCreator().Create<IAccountGroupManager>(); //把实体绑定到BindingSource accountGroupModelBindingSource.DataSource = models; models.ListChanged += new EventHandler<ListChangedEventArgs<AccountGroupModel>>(models_ListChanged); CancelEdit(); } void models_ListChanged(object sender, ListChangedEventArgs<AccountGroupModel> e) { btnUndo.Enabled = models.CanUndo; btnRedo.Enabled = models.CanRedo; btnSave.Enabled = models.HasChanged; } void LoadData() { //加载上级组别的数据 ValueTextList parent = manager.GetGroupList(); parent.Insert(0, new ValueTextPair(0, "")); parentBindingSource.DataSource = parent; //加载实现的数据 AccountGroupModels source = manager.GetModels(txtName.Text); models.Clear(); models.BeginInit();//加载时不触发绑定的事件 models.AddRange(source); models.EndInit(); accountGroupModelBindingSource.ResetBindings(false); CancelEdit(); } void BeginEdit() { models.BeginEdit(); grid.ReadOnly = false; btnNew.Enabled = true; btnDelete.Enabled = true; btnEdit.Enabled = false; btnCancel.Enabled = true; grid.StateNormal.DataCell.Back.Color1 = Color.Empty; idDataGridViewTextBoxColumn.ReadOnly = true; updateDateDataGridViewTextBoxColumn.ReadOnly = true; updateUserDataGridViewTextBoxColumn.ReadOnly = true; } void CancelEdit() { models.CancelEdit(); grid.ReadOnly = true; btnNew.Enabled = false; btnDelete.Enabled = false; btnSave.Enabled = false; btnUndo.Enabled = false; btnRedo.Enabled = false; btnEdit.Enabled = true; btnCancel.Enabled = false; pnlErrorInfo.Visible = false; grid.StateNormal.DataCell.Back.Color1 = SystemColors.Info; accountGroupModelBindingSource.ResetBindings(false); } #region 工具条按钮事件 private void btnSearch_Click(object sender, EventArgs e) { LoadData(); } private void btnClear_Click(object sender, EventArgs e) { txtName.Clear(); } private void btnEdit_Click(object sender, EventArgs e) { BeginEdit(); } private void btnCancel_Click(object sender, EventArgs e) { CancelEdit(); } private void btnSave_Click(object sender, EventArgs e) { try { grid.EndEdit(); pnlErrorInfo.Visible = false;//隐藏错误信息 foreach (DataGridViewRow r in grid.Rows) { foreach (DataGridViewCell c in r.Cells) c.ErrorText = ""; } //获取需要保存的实体列表 List<AccountGroupModel> changed = models.GetChangedItems(); manager.Save(changed, "Demo"); LoadData(); } catch (Exception exc) { if (exc is ValidationException)//显示验证的异常信息 { pnlErrorInfo.Visible = true; lblErrorInfo.Text = ""; ValidationException ve = (ValidationException)exc; foreach (ErrorInfo error in ve.ErrorInfos) { if (error.RowNum > -1) { string text = error.Errors.ToString(); grid.Rows[error.RowNum].Cells[FindColumn(grid, error.FiledName).Name] .ErrorText = text; lblErrorInfo.Text += "[Row:" + (error.RowNum + 1) + "] " + error.FiledName + ":" + text + ";\r\n"; } } } else MessageBox.Show(exc.ToString(), "Error"); } } private void btnNew_Click(object sender, EventArgs e) { accountGroupModelBindingSource.AddNew(); } private void btnDelete_Click(object sender, EventArgs e) { if (grid.SelectedRows.Count > 0) { foreach (DataGridViewRow row in grid.SelectedRows) accountGroupModelBindingSource.Remove(row.DataBoundItem); } else if (accountGroupModelBindingSource.Current != null) accountGroupModelBindingSource.Remove(accountGroupModelBindingSource.Current); } private void btnUndo_Click(object sender, EventArgs e) { grid.EndEdit(); if (models.CanUndo)//撤销 SetFocused(models.Undo()); } private void btnRedo_Click(object sender, EventArgs e) { grid.EndEdit(); if (models.CanRedo)//恢复 SetFocused(models.Redo()); } #endregion /// <summary> /// 聚焦到发生变化的行或者单元格 /// </summary> /// <param name="edited"></param> void SetFocused(EditedObject<AccountGroupModel> edited) { accountGroupModelBindingSource.ResetBindings(false); grid.ClearSelection(); if (edited.NewState == DataState.Modified) { int index = models.IndexOf(edited.DataObject); grid.Rows[index].Cells[FindColumn(grid, edited.PropertyName).Name].Selected = true; } else { int index = models.IndexOf(edited.DataObject); if (index != -1) grid.Rows[index].Selected = true; } } private DataGridViewColumn FindColumn(DataGridView grid, string property) { //根据属性名查找DataGridViewColumn foreach (DataGridViewColumn col in grid.Columns) { if (col.DataPropertyName == property) return col; } return null; } private void grid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { int index = 1; foreach (DataGridViewRow r in grid.Rows)//显示表格的行号 r.HeaderCell.Value = index++; } }
7. 运行结果
示例代码下载EntityDemo.zip
【LeanEAP.NET】精益企业应用平台----系列目录
相关文章推荐
- 【LeanEAP.NET】精益企业应用平台实战----构造DataAccessor与增删改查
- 【LeanEAP.NET】精益企业应用平台设计----功能设计
- 【LeanEAP.NET】精益企业应用平台实战----创建AccountGroup表和DTO
- 【LeanEAP.NET】精益企业应用平台设计----WebForm的UI布局
- 【LeanEAP.NET】精益企业应用平台
- 【LeanEAP.NET】精益企业应用平台设计----数据访问类库EAP.Data
- 【LeanEAP.NET】精益企业应用平台设计----WebForm的UI布局
- 【LeanEAP.NET】精益企业应用平台---源码&Demo下载
- 企业集群平台架构实现与应用实战
- 在ASP.NET中应用Excel:(8)为HTML表格增加编辑功能
- 企业集群平台架构实现与应用实战(HAproxy+keepalived篇) 推荐
- 表格批量编辑与Undo/Redo功能实现
- 在ASP.NET中应用Excel:(8)为HTML表格增加编辑功能
- vue+element的表格实现批量删除功能
- ASP.NET中应用Excel:(9)实现表格的自动增长
- 应用框架的设计与实现——.NET平台(4.3 SAF代码分析)
- Asp.Net平台下的图片在线裁剪功能的实现
- 使用Java来实现编辑器的Undo Redo功能
- 实现编辑器的Undo Redo功能用Java来
- [原创]文本编辑中利用栈来实现undo和redo的无限次操作