在GridView中的批量删除!

简介: 1.通过GridView的属性:DataKeyNames来获取主键; 2.遍历数据行,获取选中的CheckBox 所属的行的主键.(有点绕口...慢慢看...) 3.拼接SQL语句; Delete 表名 where id in(XX,XX,XX);   Default.

1.通过GridView的属性:DataKeyNames来获取主键;

2.遍历数据行,获取选中的CheckBox 所属的行的主键.(有点绕口...慢慢看...)

3.拼接SQL语句; Delete 表名 where id in(XX,XX,XX);

 

Default.aspx页完整代码:

img_405b18b4b6584ae338e0f6ecaf736533.gif View Code
<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeBehind = " Default.aspx.cs "  Inherits = " WebApplication1.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 ></ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< br  />
        
< asp:Button  ID ="btnDelete"  runat ="server"  Text ="删除选中项"  OnClick ="btnDelete_Click"
            Style
="height: 21px"   />
        
< br  />
        
< br  />
       
<% --  通过给GridView1添加属性:DataKeyNames来获取主键; -- %>
        
< asp:GridView  ID ="GridView1"  runat ="server"  AutoGenerateColumns ="false"  
            Width
="881px"  DataKeyNames ="id" >
            
< Columns >
                
< asp:BoundField  DataField ="id"  HeaderText ="编号"   />
                
< asp:BoundField  DataField ="uname"  HeaderText ="名称"   />
                
< asp:TemplateField  HeaderText ="选择" >
                    
< ItemTemplate >
                        
< asp:CheckBox  ID ="cbxId"  runat ="Server"   />
                    
</ ItemTemplate >
                
</ asp:TemplateField >
            
</ Columns >
        
</ asp:GridView >
    
</ div >
    
</ form >
</ body >
</ html >

 

Default.aspx.cs完整代码:

img_405b18b4b6584ae338e0f6ecaf736533.gif View Code
using  System;
using  System.Web.UI.WebControls;
using  System.Data;

namespace  WebApplication1
{
    
public   partial   class  Default : System.Web.UI.Page
    {
        
protected   void  Page_Load( object  sender, EventArgs e)
        {
            
if  ( ! this .IsPostBack)
            {
                GetData();
            }
        }

        
// 绑定数据
         protected   void  GetData()
        {
            
string  sql  =   " select * from userinfo " ;
            GridView1.DataSource 
=  SQLHelper.GetDateSet(sql, CommandType.Text);
            GridView1.DataBind();
        }

        
protected   void  btnDelete_Click( object  sender, EventArgs e)
        {
            
// sqlText用于拼接SQL语句;
             string  sqlText  =   " ( " ;
            
foreach  (GridViewRow objGVR  in   this .GridView1.Rows)
            {
                
// 判断当前行是否为数据行;
                 if  (objGVR.RowType  ==  DataControlRowType.DataRow)
                {
                    CheckBox objCB 
=  objGVR.FindControl( " cbxId " as  CheckBox;

                    
if  (objCB.Checked)
                    {
                        
// 获取选中行的主键;
                        sqlText  +=   this .GridView1.DataKeys[objGVR.RowIndex][ " id " ].ToString()  +   " , " ;
                    }
                }
            }

            
// 去掉最后的逗号,并且加上右括号 ,如果不去掉最后一个逗号变会成这样(1,2,3,4,5,6,)
            sqlText  =  sqlText.Substring( 0 , sqlText.Length  -   1 +   " ) " ;

            sqlText 
=   " delete userinfo where id in "   +  sqlText;

            Response.Write(
" 拼接后的SQL语句为: "   +  sqlText);
            
// 执行删除语句 
             int  delCount  =  Convert.ToInt32(SQLHelper.ExecuteNonQuery(sqlText, CommandType.Text));

            Response.Write(
" 共删除数据: "   +  delCount  +   " " );

            
this .GetData();


        }
    }
}

 

如需全选功能请参见:http://www.cnblogs.com/zhuiyi/archive/2011/06/27/2091738.html

目录
相关文章
|
.NET 开发框架 Go
GridView控件自定义分页的实现
前人栽树,后人乘凉,话不多说,代码如下:     实现方式一: .aspx: [c-sharp] view plain copy <form id="form1" runat="server">       <table style="width: 605px">         .
1412 0
列表ListBox、ListView、GridView 排序
列表排序 1.使用控件默认排序方式(推荐) ListControl.Items.SortDescriptions.Clear(); ListControl.Items.SortDescriptions.
869 0
|
.NET 开发框架 JavaScript
listview添加行
转自博客http://blog.csdn.net/hyouq110/article/details/6057179 ListView lv = new ListView (); //添加一行的方法 ListViewItem item = lv.
742 0