All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
ASP.NET技巧:DataGrid的多行提交_[Asp.Net教程]

Writer: delv Article type: Programming skills(编程技巧) Time: 2014/1/8 7:02:03 Browse times: 319 Comment times: 0

ASP.NET技巧:DataGrid的多行提交_[Asp.Net教程]


Head photo

Go homepage
Upload pictures
Write articles

ASP.NET技巧:DataGrid的多行提交_[Asp.Net教程]

尽管ASP.NET DataGrid是众所周知非常好的表格控件,不过,提起DataGrid的编辑功能,我们却不敢恭维了,就拿DataGrid的数据提交功能来说,的确存在很大的问题:在DataGrid中,每编辑一行就要提交一行,即所谓“单行编辑、单行提交”,这样的话,如果编辑的行数过多,不仅用户操作繁琐,还会造成对服务器的频繁访问,极大降低系统效率。

当然了,有一种借尸还魂的解决方法,那就是把所要编辑的内容转到其他的页中在TextBox中进行编辑。不过,仔细想想,这种方法难道不是自己在骗自己吗,还有在Grid中我们编辑的时候总不能老是用Tab键来实现Grid(TextBox)之间的跳转吧,如果响应回车事件,那么需要程序员浪费很大的精力来开发。

如何解决上述问题呢?下面我向大家推荐一个我正在使用的国产DataGrid:SmartGrid(天空软件站可以下载:http://www.skycn.com/soft/23547.html ),这个控件我已经用了好长的时间了,现在来同大家探讨一下SmartGrid的多行提交的方法:SmartGrid并没有DataGrid中的那些按钮列而是整个的表单只有一个提交按钮,无论你更改了一行或者是多行都可以一次性的提交,下面来随便看点例子:

实例:

上图中是一个比较好的编辑的例子,例子显示,你可以编辑多行也可以编辑一行,然后一起进行提交。

代码:

修改按钮的代码:

private void btonSave_Click(object sender, System.EventArgs e)

{

this.DataGrid1.ReadOnly = false;//进入编辑

this.DataGrid1.AllowAdd = true;//允许添加

this.DataGrid1.AllowDelete = true;//允许删除

}

此段代码是smartgrid的独有的属性你可以设添加删除 编辑 的各种的功能

保存按钮的代码:
private void Button2_Click(object sender, System.EventArgs e)

{

DataTable t = (DataTable)this.SmartGrid1.DataSource;

this.sqlDataAdapter1.Update(t);

t.Clear();

this.sqlDataAdapter1.Fill(t);

this.SmartGrid1.DataSource = t;

}

这是整体的把数据提交到数据库中,这种做法适合大数据量的情况

还有一种是数据逐行的提交到服务器

代码:

private void btonSave_Click(object sender, System.EventArgs e)

{

DataTable tb=(DataTable)this.SmartGrid1.DataSource;

SqlParameter[] parameters=new SqlParameter[5];

foreach(DataRow dr in tb.Rows)

{

parameters[0]=new SqlParameter("@customerId",""+dr[1]+"");

parameters[1]=new SqlParameter("@companyName",""+dr[0]+"");

parameters[2]=new SqlParameter("@contactName",""+dr[2]+"");

parameters[3]=new SqlParameter("@contactTitle",""+dr[3]+"");

parameters[4]=new SqlParameter("@address",""+dr[4]+"");

//EamPd 是类Execute是执行存储过程的函数parameters是存储过程所需要的参数

EamPd.Execute("CreatLayer",parameters);

}

}

来源:网络





There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.