您的位置:首页 > 其它

表格批量编辑与Undo/Redo功能实现

2012-06-02 11:11 429 查看
利用EAP.Entity提供的可以Undo/Redo的列表绑定数据到DataGridView,让表格批量编辑更方便更高效。

1. 还是利用AccountGroup表的结构,然后得到实体AccountGroupModel:

1     [System.Serializable()]
2     public class AccountGroupModels : System.Collections.Generic.List<AccountGroupModel>
3     {
4
5         public AccountGroupModels()
6         {
7         }
8
9         public AccountGroupModels(System.Collections.Generic.IEnumerable<AccountGroupModel> collection) :
10             base(collection)
11         {
12         }
13     }
14
15     /// <summary>
16     /// <para></para>
17     /// </summary>
18     [System.Serializable()]
19     public partial class AccountGroupModel : EAP.Entity.DataObject<AccountGroupModel>
20     {
21         #region Member Property Region
22         /// <summary>
23         /// <para>主键</para>
24         /// </summary>
25         [EAP.Data.ColumnInfoAttribute(Visible = false, Queryable = true, DbColumn = "Id")]
26         public int Id
27         {
28             get
29             {
30                 return GetValue<int>("Id");
31             }
32             set
33             {
34                 SetValue("Id", value);
35             }
36         }
37
38         /// <summary>
39         /// <para>名称</para>
40         /// </summary>
41         [EAP.Data.ColumnInfoAttribute(DbColumn = "Name")]
42         public string Name
43         {
44             get
45             {
46                 return GetValue<string>("Name");
47             }
48             set
49             {
50                 SetValue("Name", value);
51             }
52         }
53
54         /// <summary>
55         /// <para>上级组别</para>
56         /// </summary>
57         [EAP.Data.ColumnInfoAttribute(DbColumn = "Parent")]
58         public int Parent
59         {
60             get
61             {
62                 return GetValue<int>("Parent");
63             }
64             set
65             {
66                 SetValue("Parent", value);
67             }
68         }
69
70         /// <summary>
71         /// <para>可见性</para>
72         /// </summary>
73         [EAP.Data.ColumnInfoAttribute(DbColumn = "Visible")]
74         public bool Visible
75         {
76             get
77             {
78                 return GetValue<bool>("Visible");
79             }
80             set
81             {
82                 SetValue("Visible", value);
83             }
84         }
85
86         /// <summary>
87         /// <para>修改者</para>
88         /// </summary>
89         [EAP.Data.ColumnInfoAttribute(DbColumn = "UpdateUser")]
90         public string UpdateUser
91         {
92             get
93             {
94                 return GetValue<string>("UpdateUser");
95             }
96             set
97             {
98                 SetValue("UpdateUser", value);
99             }
100         }
101
102         /// <summary>
103         /// <para>修改时间</para>
104         /// </summary>
105         [EAP.Data.ColumnInfoAttribute(DbColumn = "UpdateDate")]
106         public System.DateTime UpdateDate
107         {
108             get
109             {
110                 return GetValue<System.DateTime>("UpdateDate");
111             }
112             set
113             {
114                 SetValue("UpdateDate", value);
115             }
116         }
117         #endregion
118     }


1     public partial class DemoForm : KryptonForm
2     {
3         //业务接口
4         IAccountGroupManager manager;
5         //实体列表
6         DataObjectList<AccountGroupModel> models = new DataObjectList<AccountGroupModel>();
7
8         public DemoForm()
9         {
10             InitializeComponent();
11             //创建接口的实现
12             manager = new RefObjectCreator().Create<IAccountGroupManager>();
13             //把实体绑定到BindingSource
14             accountGroupModelBindingSource.DataSource = models;
15             models.ListChanged += new EventHandler<ListChangedEventArgs<AccountGroupModel>>(models_ListChanged);
16             CancelEdit();
17         }
18
19         void models_ListChanged(object sender, ListChangedEventArgs<AccountGroupModel> e)
20         {
21             btnUndo.Enabled = models.CanUndo;
22             btnRedo.Enabled = models.CanRedo;
23             btnSave.Enabled = models.HasChanged;
24         }
25
26         void LoadData()
27         {
28             //加载上级组别的数据
29             ValueTextList parent = manager.GetGroupList();
30             parent.Insert(0, new ValueTextPair(0, ""));
31             parentBindingSource.DataSource = parent;
32             //加载实现的数据
33             AccountGroupModels source = manager.GetModels(txtName.Text);
34             models.Clear();
35             models.BeginInit();//加载时不触发绑定的事件
36             models.AddRange(source);
37             models.EndInit();
38             accountGroupModelBindingSource.ResetBindings(false);
39             CancelEdit();
40         }
41
42         void BeginEdit()
43         {
44             models.BeginEdit();
45             grid.ReadOnly = false;
46             btnNew.Enabled = true;
47             btnDelete.Enabled = true;
48             btnEdit.Enabled = false;
49             btnCancel.Enabled = true;
50             grid.StateNormal.DataCell.Back.Color1 = Color.Empty;
51             idDataGridViewTextBoxColumn.ReadOnly = true;
52             updateDateDataGridViewTextBoxColumn.ReadOnly = true;
53             updateUserDataGridViewTextBoxColumn.ReadOnly = true;
54         }
55
56         void CancelEdit()
57         {
58             models.CancelEdit();
59             grid.ReadOnly = true;
60             btnNew.Enabled = false;
61             btnDelete.Enabled = false;
62             btnSave.Enabled = false;
63             btnUndo.Enabled = false;
64             btnRedo.Enabled = false;
65             btnEdit.Enabled = true;
66             btnCancel.Enabled = false;
67             pnlErrorInfo.Visible = false;
68             grid.StateNormal.DataCell.Back.Color1 = SystemColors.Info;
69             accountGroupModelBindingSource.ResetBindings(false);
70         }
71
72         #region 工具条按钮事件
73
74         private void btnSearch_Click(object sender, EventArgs e)
75         {
76             LoadData();
77         }
78
79         private void btnClear_Click(object sender, EventArgs e)
80         {
81             txtName.Clear();
82         }
83
84         private void btnEdit_Click(object sender, EventArgs e)
85         {
86             BeginEdit();
87         }
88
89         private void btnCancel_Click(object sender, EventArgs e)
90         {
91             CancelEdit();
92         }
93
94         private void btnSave_Click(object sender, EventArgs e)
95         {
96             try
97             {
98                 grid.EndEdit();
99                 pnlErrorInfo.Visible = false;//隐藏错误信息
100                 foreach (DataGridViewRow r in grid.Rows)
101                 {
102                     foreach (DataGridViewCell c in r.Cells)
103                         c.ErrorText = "";
104                 }
105                 //获取需要保存的实体列表
106                 List<AccountGroupModel> changed = models.GetChangedItems();
107                 manager.Save(changed, "Demo");
108                 LoadData();
109             }
110             catch (Exception exc)
111             {
112                 if (exc is ValidationException)//显示验证的异常信息
113                 {
114                     pnlErrorInfo.Visible = true;
115                     lblErrorInfo.Text = "";
116                     ValidationException ve = (ValidationException)exc;
117                     foreach (ErrorInfo error in ve.ErrorInfos)
118                     {
119                         if (error.RowNum > -1)
120                         {
121                             string text = error.Errors.ToString();
122                             grid.Rows[error.RowNum].Cells[FindColumn(grid, error.FiledName).Name]
123                                 .ErrorText = text;
124                             lblErrorInfo.Text += "[Row:" + (error.RowNum + 1) + "] "
125                                 + error.FiledName + ":" + text + ";\r\n";
126                         }
127                     }
128                 }
129                 else
130                     MessageBox.Show(exc.ToString(), "Error");
131             }
132         }
133
134         private void btnNew_Click(object sender, EventArgs e)
135         {
136             accountGroupModelBindingSource.AddNew();
137         }
138
139         private void btnDelete_Click(object sender, EventArgs e)
140         {
141             if (grid.SelectedRows.Count > 0)
142             {
143                 foreach (DataGridViewRow row in grid.SelectedRows)
144                     accountGroupModelBindingSource.Remove(row.DataBoundItem);
145             }
146             else if (accountGroupModelBindingSource.Current != null)
147                 accountGroupModelBindingSource.Remove(accountGroupModelBindingSource.Current);
148
149         }
150
151         private void btnUndo_Click(object sender, EventArgs e)
152         {
153             grid.EndEdit();
154             if (models.CanUndo)//撤销
155                 SetFocused(models.Undo());
156         }
157
158         private void btnRedo_Click(object sender, EventArgs e)
159         {
160             grid.EndEdit();
161             if (models.CanRedo)//恢复
162                 SetFocused(models.Redo());
163         }
164
165         #endregion
166
167         /// <summary>
168         /// 聚焦到发生变化的行或者单元格
169         /// </summary>
170         /// <param name="edited"></param>
171         void SetFocused(EditedObject<AccountGroupModel> edited)
172         {
173             accountGroupModelBindingSource.ResetBindings(false);
174             grid.ClearSelection();
175             if (edited.NewState == DataState.Modified)
176             {
177                 int index = models.IndexOf(edited.DataObject);
178                 grid.Rows[index].Cells[FindColumn(grid, edited.PropertyName).Name].Selected = true;
179             }
180             else
181             {
182                 int index = models.IndexOf(edited.DataObject);
183                 if (index != -1)
184                     grid.Rows[index].Selected = true;
185             }
186         }
187
188         private DataGridViewColumn FindColumn(DataGridView grid, string property)
189         {
190             //根据属性名查找DataGridViewColumn
191             foreach (DataGridViewColumn col in grid.Columns)
192             {
193                 if (col.DataPropertyName == property)
194                     return col;
195             }
196             return null;
197         }
198
199         private void grid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
200         {
201             int index = 1;
202             foreach (DataGridViewRow r in grid.Rows)//显示表格的行号
203                 r.HeaderCell.Value = index++;
204         }
205     }




7. 运行结果



示例代码下载EntityDemo.zip

【LeanEAP.NET】精益企业应用平台----系列目录
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: