1 概述
这篇文章只是我在近期开发的一个小系统里应用LINQ TO SQL的其中一个总结。
我做的系统是一个奖金核算系统,其中有许多基础数据需要用户去维护,有些简单数据,如支出项目的维护,费别项目维护,部门信息维护其实都是一个个单独的数据表,只要提供类似于SQL SERVER里查看表数据的模式就可以满足用户的需求。
2 思路
其实在.NET2.0时,就可以用绑定DataTable的方式来实现这个功能,只是现在用LINQ TO SQL换一个外衣的方式来实现,不过在某些细节上还是有不同的。本系统首先定义了一个窗体基类,把界面框架定义好,和一些常用操作定义,如增删改查,然后具体实现窗体,则派生自该基类,派生窗体主要职责是定义具体的网格列,定义具体查询的数据,如果有特殊的操作也可以在该窗体添加。
3 实现
(1)窗体基类界面
(2)窗体基类代码
窗体基类实现代码
public partial class BaseForm : Form
{
protected BonusDataContext mydc;//DataContext对象
protected bool IsModify=false;//修改标记
public BaseForm()
{
InitializeComponent();
}
/**//// <summary>
/// 数据绑定虚方法
/// </summary>
protected virtual void GridBind()
{
}
/**//// <summary>
/// 退出按钮处理事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void tsbtnExit_Click(object sender, EventArgs e)
{
if (IsModify)
{
DialogResult result = MessageBox.Show("有修改的数据未保存,是否保存?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
dgvData.CommitEdit(DataGridViewDataErrorContexts.Commit);
mydc.SubmitChanges();
this.Close();
}
else if (result == DialogResult.No)
{
this.Close();
mydc.Dispose();
}
}
else
{
this.Close();
mydc.Dispose();
}
GC.Collect();
}
//新增数据
protected virtual void tsbtnAdd_Click(object sender, EventArgs e)
{
dgvData.CurrentCell = dgvData[1, dgvData.Rows.Count - 1];
}
//保存数据
protected virtual void tsbtnSave_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("是否保存?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
try
{
dgvData.CommitEdit(DataGridViewDataErrorContexts.Commit);
mydc.SubmitChanges();
IsModify = false;
}
catch (DuplicateKeyException)
{
MessageBox.Show("提交数据存在重复数据,保存失败");
}
}
}
//刷新数据,回归到编辑前状态
protected virtual void tsbtnRefresh_Click(object sender, EventArgs e)
{
IsModify = false;
mydc = new BonusDataContext();
GridBind();
}
//删除数据
protected virtual void tsbtnDel_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("删除的数据无法恢复,是否删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
dgvData.CommitEdit(DataGridViewDataErrorContexts.Commit);
for (int i = 0; i < dgvData.Rows.Count; i++)
{
if (dgvData["colSelect", i].Value != null)
{
if (dgvData["colSelect", i].Value.ToString() == "1")
{
dgvData.Rows.Remove(dgvData.Rows[i]);
}
}
}
mydc.SubmitChanges();
}
}
//取消当前编辑数据
protected virtual void tsbtnCancel_Click(object sender, EventArgs e)
{
dgvData.CancelEdit();
}
protected virtual void dgvData_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
IsModify = true;
}
}
public partial class BaseForm : Form
{
protected BonusDataContext mydc;//DataContext对象
protected bool IsModify=false;//修改标记
public BaseForm()
{
InitializeComponent();
}
/**//// <summary>
/// 数据绑定虚方法
/// </summary>
protected virtual void GridBind()
{
}
/**//// <summary>
/// 退出按钮处理事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void tsbtnExit_Click(object sender, EventArgs e)
{
if (IsModify)
{
DialogResult result = MessageBox.Show("有修改的数据未保存,是否保存?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
dgvData.CommitEdit(DataGridViewDataErrorContexts.Commit);
mydc.SubmitChanges();
this.Close();
}
else if (result == DialogResult.No)
{
this.Close();
mydc.Dispose();
}
}
else
{
this.Close();
mydc.Dispose();
}
GC.Collect();
}
//新增数据
protected virtual void tsbtnAdd_Click(object sender, EventArgs e)
{
dgvData.CurrentCell = dgvData[1, dgvData.Rows.Count - 1];
}
//保存数据
protected virtual void tsbtnSave_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("是否保存?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
try
{
dgvData.CommitEdit(DataGridViewDataErrorContexts.Commit);
mydc.SubmitChanges();
IsModify = false;
}
catch (DuplicateKeyException)
{
MessageBox.Show("提交数据存在重复数据,保存失败");
}
}
}
//刷新数据,回归到编辑前状态
protected virtual void tsbtnRefresh_Click(object sender, EventArgs e)
{
IsModify = false;
mydc = new BonusDataContext();
GridBind();
}
//删除数据
protected virtual void tsbtnDel_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("删除的数据无法恢复,是否删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
dgvData.CommitEdit(DataGridViewDataErrorContexts.Commit);
for (int i = 0; i < dgvData.Rows.Count; i++)
{
if (dgvData["colSelect", i].Value != null)
{
if (dgvData["colSelect", i].Value.ToString() == "1")
{
dgvData.Rows.Remove(dgvData.Rows[i]);
}
}
}
mydc.SubmitChanges();
}
}
//取消当前编辑数据
protected virtual void tsbtnCancel_Click(object sender, EventArgs e)
{
dgvData.CancelEdit();
}
protected virtual void dgvData_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
IsModify = true;
}
}
(3)派生窗体界面,此处以成本数据维护为例
(4)派生窗体代码实现
派生窗体实现
public partial class frmCostManage : Hqyy.BonusAccountingSystem.WinUI.BaseForm
{
//定义列
protected System.Windows.Forms.DataGridViewTextBoxColumn colCostID;
protected System.Windows.Forms.DataGridViewTextBoxColumn colCostName;
protected System.Windows.Forms.DataGridViewTextBoxColumn colPyCode;
protected System.Windows.Forms.DataGridViewTextBoxColumn colOrderList;
protected System.Windows.Forms.DataGridViewTextBoxColumn colIsUse;
protected System.Windows.Forms.DataGridViewCheckBoxColumn colIsCopy;
public frmCostManage()
{
InitializeComponent();
this.Text = "成本数据管理";
}
protected override void GridBind()
{
dgvData.DataSource = from ds in mydc.Costs
orderby ds.orderlist
select ds;
}
private void frmCostManage_Load(object sender, EventArgs e)
{
mydc = new BonusDataContext();
InitControl();
GridBind();
}
//初始化控件
private void InitControl()
{
colCostID = new DataGridViewTextBoxColumn();
colCostName = new DataGridViewTextBoxColumn();
colIsUse = new DataGridViewTextBoxColumn();
colOrderList = new DataGridViewTextBoxColumn();
colPyCode = new DataGridViewTextBoxColumn();
colIsCopy = new DataGridViewCheckBoxColumn();
// colCostID
//
this.colCostID.DataPropertyName = "CostID";
this.colCostID.HeaderText = "编码";
this.colCostID.Name = "colCostID";
this.colCostID.Width = 80;
//
// colCostName
//
this.colCostName.DataPropertyName = "CostName";
this.colCostName.HeaderText = "成本名称";
this.colCostName.Name = "colCostName";
this.colCostName.Width = 150;
//
//colIsUse
//
this.colIsUse.DataPropertyName = "IsUse";
this.colIsUse.HeaderText = "有效";
this.colIsUse.Name = "colIsUse";
this.colIsUse.Width = 80;
//
// colOrderList
//
this.colOrderList.DataPropertyName = "OrderList";
this.colOrderList.HeaderText = "排序";
this.colOrderList.Name = "colOrderList";
this.colOrderList.Width = 80;
//
// colOrgTypeName
//
this.colPyCode.DataPropertyName = "PyCode";
this.colPyCode.HeaderText = "拼音码";
this.colPyCode.Name = "colPyCode";
this.colPyCode.Width = 150;
//
//colIsCopy
//
this.colIsCopy.FalseValue = "0";
this.colIsCopy.HeaderText = "是否复制上月数据";
this.colIsCopy.Name = "colIsCopy";
this.colIsCopy.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.colIsCopy.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.colIsCopy.TrueValue = "1";
this.colIsCopy.DataPropertyName = "iscopy";
//将列加入到gird里
this.dgvData.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.colCostID,
this.colCostName,
this.colPyCode,
colOrderList,
colIsUse,
colIsCopy});
}
}
public partial class frmCostManage : Hqyy.BonusAccountingSystem.WinUI.BaseForm
{
//定义列
protected System.Windows.Forms.DataGridViewTextBoxColumn colCostID;
protected System.Windows.Forms.DataGridViewTextBoxColumn colCostName;
protected System.Windows.Forms.DataGridViewTextBoxColumn colPyCode;
protected System.Windows.Forms.DataGridViewTextBoxColumn colOrderList;
protected System.Windows.Forms.DataGridViewTextBoxColumn colIsUse;
protected System.Windows.Forms.DataGridViewCheckBoxColumn colIsCopy;
public frmCostManage()
{
InitializeComponent();
this.Text = "成本数据管理";
}
protected override void GridBind()
{
dgvData.DataSource = from ds in mydc.Costs
orderby ds.orderlist
select ds;
}
private void frmCostManage_Load(object sender, EventArgs e)
{
mydc = new BonusDataContext();
InitControl();
GridBind();
}
//初始化控件
private void InitControl()
{
colCostID = new DataGridViewTextBoxColumn();
colCostName = new DataGridViewTextBoxColumn();
colIsUse = new DataGridViewTextBoxColumn();
colOrderList = new DataGridViewTextBoxColumn();
colPyCode = new DataGridViewTextBoxColumn();
colIsCopy = new DataGridViewCheckBoxColumn();
// colCostID
//
this.colCostID.DataPropertyName = "CostID";
this.colCostID.HeaderText = "编码";
this.colCostID.Name = "colCostID";
this.colCostID.Width = 80;
//
// colCostName
//
this.colCostName.DataPropertyName = "CostName";
this.colCostName.HeaderText = "成本名称";
this.colCostName.Name = "colCostName";
this.colCostName.Width = 150;
//
//colIsUse
//
this.colIsUse.DataPropertyName = "IsUse";
this.colIsUse.HeaderText = "有效";
this.colIsUse.Name = "colIsUse";
this.colIsUse.Width = 80;
//
// colOrderList
//
this.colOrderList.DataPropertyName = "OrderList";
this.colOrderList.HeaderText = "排序";
this.colOrderList.Name = "colOrderList";
this.colOrderList.Width = 80;
//
// colOrgTypeName
//
this.colPyCode.DataPropertyName = "PyCode";
this.colPyCode.HeaderText = "拼音码";
this.colPyCode.Name = "colPyCode";
this.colPyCode.Width = 150;
//
//colIsCopy
//
this.colIsCopy.FalseValue = "0";
this.colIsCopy.HeaderText = "是否复制上月数据";
this.colIsCopy.Name = "colIsCopy";
this.colIsCopy.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.colIsCopy.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.colIsCopy.TrueValue = "1";
this.colIsCopy.DataPropertyName = "iscopy";
//将列加入到gird里
this.dgvData.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.colCostID,
this.colCostName,
this.colPyCode,
colOrderList,
colIsUse,
colIsCopy});
}
}
4 总结
这是一个最简单的实现,其实还有很多地方要完善的,如输入的合法性判断,数据筛选等,而且这种方法只适合于单表的数据维护,如果显示数据涉及到多个表,此法则无能为力,但在我下一篇文章将会介绍用虚拟模式来实现多表显示数据批编辑数据的方法。