您的位置:首页 > 其它

一步一步教你使用AgileEAS.NET基础类库进行应用开发-WinForm应用篇-演示ORM对象与DataGridView的绑定技术-商品字典的另一个实现

2010-11-12 08:41 1066 查看

[b]回顾与说明[/b]

前面我们把“商品字典”、“商品入库”、“商品库存查询”、“商品入库查询”四个模块已经概括或者详细的演示了一个管理信息系统的典型应用场景,按照原来的打算,WinForm篇的例子系统中的几个模块就告一段落了。

由于好多朋友都问我,你的例子中大量使用ListView控件,很想知道是否可以支持DataGridView控件,所以我就有想到重新用DataGridView写一下“商品字典”模块。

本文内容

关于“商品字典”的实现及其业务应用场景请参见一步一步教你使用AgileEAS.NET基础类库进行应用开发-WinForm应用篇-实例一个模块(商品字典)一文。

今天本文的主要内容是AgileEAS.NET平台中的ORM对象与DataGridView的绑定,在AgileEAS.NET平台的ORM体系之中,有一个ITable接口,他继承了数据绑定接口IListSource,并且ITable的Rows属性为EntityCollection对象本身就是一个List<IEntity>,那么通过ITable.Rows也是可以实现数据绑定的。

本文的例子中,我只演示商品字典数据的绑定与修改,并且修改也使用了一个偷懒的方法,不是最优的实现,另外关于字典的删除和增加我也没有实现,有兴趣的朋友自己实现吧。

下面我们就来开始干活吧,第一件事,还是拖控件堆界面。

制做界面

首先,我们需要在UI项目中增加一个WinForm窗体ProductDictForm拖动控件达到如下效果:



在这里,我们需要注意的是需要向界面放一个dataGridView,并且设置一下他的列,当然了大家也可以直接使用BindingSource绑定到Product.DAL.Interface.IProduct之上。

编写绑定代码

下面我们来写“查询”、“打印”两个按钮的事件处理代码:

voidLoadDictList()
{
currentDict=DALHelper.DALManager.CreateProduct();
dictList=DALHelper.DALManager.CreateProductList();
dictList.GetProductList(this.tbSearch.Text);
//两种方式都支持
this.dataGridView1.DataSource=dictList;
//this.dataGridView1.DataSource=dictList.Rows;
}
privatevoidbtnSearch_Click(objectsender,EventArgse)
{
this.LoadDictList();
}
privatevoidbtnPrint_Click(objectsender,EventArgse)
{
if(dictList==null)
{
MessageBox.Show("没有需要打印的数据!","提?示?",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
if(this.printForm==null)
this.printForm=newPrintViewDialog();
//
System.IO.TextReadertextReader=null;
try
{
stringfileName=Path.Combine(Application.StartupPath,"Reports\\商品字典.rdl");
textReader=newSystem.IO.StreamReader(fileName);
this.printForm.SourceRdl=textReader.ReadToEnd();
}
finally
{
if(textReader!=null)
textReader.Close();
}
this.printForm.DataObject=dictList;
this.printForm.PrintPreview();
}


.codearea{color:black;background-color:white;line-height:18px;border:1pxsolid#4f81bd;margin:0;width:auto!important;width:100%;overflow:auto;text-align:left;font-size:12px;font-family:"CourierNew","Consolas","Fixedsys","BitStreamVeraSansMono",courier,monospace,serif}
.codeareapre{color:black;line-height:18px;padding:00012px!important;margin:0em;background-color:#fff!important}
.linewrappre{white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;word-break:normal}
.codeareapre.alt{background-color:#f7f7ff!important}
.codearea.lnum{color:#4f81bd;line-height:18px}

接下来看看编辑处理代码:

privatevoiddataGridView1_CellBeginEdit(objectsender,DataGridViewCellCancelEventArgse)
{
if(this.dataGridView1.CurrentRow!=null)
{
IProductproduct=this.dataGridView1.CurrentRow.DataBoundItemasIProduct;
currentDict.Code=product.Code;
}
}
privatevoiddataGridView1_CellEndEdit(objectsender,DataGridViewCellEventArgse)
{
IProductproduct=this.dataGridView1.CurrentRow.DataBoundItemasIProduct;
if(product.Code==currentDict.Code)
product.Update();
else
{
product.Update();
product.Idn=product.GetMaxNewIdn();
currentDict.Delete();
product.Insert();
}
}


.codearea{color:black;background-color:white;line-height:18px;border:1pxsolid#4f81bd;margin:0;width:auto!important;width:100%;overflow:auto;text-align:left;font-size:12px;font-family:"CourierNew","Consolas","Fixedsys","BitStreamVeraSansMono",courier,monospace,serif}
.codeareapre{color:black;line-height:18px;padding:00012px!important;margin:0em;background-color:#fff!important}
.linewrappre{white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;word-break:normal}
.codeareapre.alt{background-color:#f7f7ff!important}
.codearea.lnum{color:#4f81bd;line-height:18px}

运行结果

编译并运行程序,我们看一下运行效果:



打印预览:





导出报表,选择导出Excel格式:



我写完这篇post,WinForm篇的例程即将就结束了,接下来,我会在WinForm篇之中安排几篇文章讲例程的部署问题,说是部署问题,其他也不是部署问题,而是例子是以何种方式运行,是直接连接数据库,还是通过服务桥接器连接到远程服务器进行业务处理。

本文我就说到这里,对AgileEAS.NET平台感兴趣的朋友呢,可以下载了完整代码之后自己看看,有问题请及时的和我联系。

有关本例所涉及的数据表结构请参考基于AgileEAS.NET平台基础类库进行应用开发-总体说明及数据定义一文,有关数据对象模型定义文件、文档、DDL脚本请下载:http://files.cnblogs.com/eastjade/demo.db.doc.sql.rar,本例完整代码下载:Product.Demo.rar。

链接

一步一步教你使用AgileEAS.NET基础类库进行应用开发-系列目录

AgileEAS.NET平台开发指南-系列目录

AgileEAS.NET应用开发平台介绍-文章索引

AgileEAS.NET平台应用开发教程-案例计划

AgileEAS.NET官方网站

敏捷软件工程实验室

QQ群:116773358
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐