当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net下gridview 批量删除的实现方法

ASP.NET
.Net中使用com组件后发生System.ArithmeticException异常的解决办法
SQL Server.net 和 OLE DB.net连接数据库的比较
后台更新DataTable行内容的方法
敏捷软件开发(原则,模式与实践)笔记1
确保文本框输入值为数值的代码
XML和数据库之间相互的映射
让你的.NET程序兼容不同版本的Dll文件。
.NET 的数据访问应用程序块(Data Access Application Block)
用控件仅一条指令实现界面换肤和多语言版本(YFSkins)
Microsoft User Interface Process Application Block 研究(3)
分享:处理Excel方法小结
基于ASP.NET实现全球化
.net 里面 protected private 的变量也可以访问(新发现)。
关于C#中{0}和{1}的问题初次在此发贴,问题对你易对我难,求救了
使用C#代码实现增加用户帐号
全世界都在关注-微软重大产品发布
教你做Rational Rose(UML Design)
OLE DB取得数据库的架构信息
VB 从零开始编外挂(三)
XPath序列之四

ASP.NET 中的 asp.net下gridview 批量删除的实现方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-11   浏览: 380 ::
收藏到网摘: n/a

要实现的效果很简单,看下图片:

功能:选中CheckBox,后从数据库中删除选中项。文章侧重将如何实现批量删除,对于如何链接数据库和绑定数据不做详细解释。
1 我们先要在GridView中添加一列为CheckBox。代码如下:
复制代码 代码如下:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="vote_id" HeaderText="编号" />
<asp:BoundField DataField="vote_name" HeaderText="名称" />
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox id="cbxId" runat="Server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

注意id为cbxId,呆会我们要用到。
2 绑定数据。
复制代码 代码如下:

//初始化数据
private void dataInit()
{
string sqlText = "select * from vote";
SqlConnection conn = getCon();
SqlDataAdapter da = new SqlDataAdapter(sqlText,conn);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds, "vote");
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[]{"vote_id"};
GridView1.DataBind();
conn.Close();
conn.Dispose();
}


3 点击“删除选中”按钮的事件。
复制代码 代码如下:

protected void Button1_Click(object sender, EventArgs e)
{
string sqlText = "(";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//搜索第n行3列
CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("cbxId");
if (cbx.Checked == true)
{
sqlText = sqlText + Convert.ToInt32(GridView1.DataKeys[i].Value) + ",";
}
}
//去掉最后的逗号,并且加上右括号
sqlText = sqlText.Substring(0,sqlText.Length - 1) + ")";
sqlText = "delete vote where vote_id in" + sqlText;
try
{
//执行删除语句
SqlConnection conn = getCon();
conn.Open();
SqlCommand cmd = new SqlCommand(sqlText,conn);
int delCount = Convert.ToInt32(cmd.ExecuteNonQuery());
Response.Write("<script>alert('共删除" + delCount + "条数据');</script>");
dataInit();
}
catch(Exception ex)
{
//若有错误发生,输出错误信息
Response.Write(ex.Message);
}
}
这里解释下:SQL语句删除这里使用的是: delete vote where vote_id in(1,3,5,6)
所以对于选中后,我们只需要取得(1,3,5,6)这样的语句就可以了。看上面代码,我稍微做了下注释。

完整的代码:
Default.aspx
复制代码 代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>gridview 批量删除</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="vote_id" HeaderText="编号" />
<asp:BoundField DataField="vote_name" HeaderText="名称" />
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox id="cbxId" runat="Server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="删除选中" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>

Default.aspx.cs
复制代码 代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//引入命名空间
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//判断回发是否要执行
if (!IsPostBack)
{
dataInit();
}
}
//返回一个连接对象
private SqlConnection getCon()
{
return new SqlConnection(ConfigurationManager.ConnectionStrings["voteConnectionString"].ToString());
}
//初始化数据
private void dataInit()
{
string sqlText = "select * from vote";
SqlConnection conn = getCon();
SqlDataAdapter da = new SqlDataAdapter(sqlText,conn);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds, "vote");
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[]{"vote_id"};
GridView1.DataBind();
conn.Close();
conn.Dispose();
}
protected void Button1_Click(object sender, EventArgs e)
{
string sqlText = "(";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//搜索第n行3列
CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("cbxId");
if (cbx.Checked == true)
{
sqlText = sqlText + Convert.ToInt32(GridView1.DataKeys[i].Value) + ",";
}
}
//去掉最后的逗号,并且加上右括号
sqlText = sqlText.Substring(0,sqlText.Length - 1) + ")";
sqlText = "delete vote where vote_id in" + sqlText;
try
{
//执行删除语句
SqlConnection conn = getCon();
conn.Open();
SqlCommand cmd = new SqlCommand(sqlText,conn);
int delCount = Convert.ToInt32(cmd.ExecuteNonQuery());
Response.Write("<script>alert('共删除" + delCount + "条数据');</script>");
dataInit();
}
catch(Exception ex)
{
//若有错误发生,输出错误信息
Response.Write(ex.Message);
}
}
}