您的位置:首页 > 其它

【LeanEAP.NET】精益企业应用平台实战----表格批量编辑与Undo/Redo功能实现

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

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