Gridview的单行删除,多行删除
时间:2011-02-25 来源:hfliyi
前台
1 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
2 onrowdatabound="GridView1_RowDataBound" onrowdeleting="GridView1_RowDeleting">
3 <Columns>
4 <asp:TemplateField HeaderText ="选择">
5 <ItemTemplate>
6 <asp:CheckBox ID="CheckBox1" runat="server" />
7 </ItemTemplate>
8 </asp:TemplateField>
9 <asp:BoundField DataField ="CustomerID" HeaderText ="CustomerID" />
10 <asp:BoundField DataField ="CompanyName" HeaderText ="CompanyName" />
11 <asp:BoundField DataField ="ContactName" HeaderText ="ContactName" />
12 <asp:BoundField DataField ="ContactTitle" HeaderText ="ContactTitle" />
13
14 <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
15
16 </Columns>
17 </asp:GridView>
18 </div>
19 <asp:CheckBox ID="CheckBox2" runat="server" Text="全选" AutoPostBack="True"
20 oncheckedchanged="CheckBox2_CheckedChanged" />
21 <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
22 Text="全部删除" />
后台代码:
后台1 protected void Page_Load(object sender, EventArgs e)
2 {
3 this.Button1.Attributes.Add("onclick", "return confirm('确认全部删除吗?');");
4 if (!IsPostBack)
5 {
6 databind();
7 }
8 }
9 public void databind()
10 {
11 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
12 SqlCommand cmd = new SqlCommand();
13 cmd.Connection = con;
14 cmd.CommandText = "Select top 10 * From Customers";
15 SqlDataAdapter da = new SqlDataAdapter(cmd);
16 DataSet ds = new DataSet();
17 da.Fill(ds);
18 this.GridView1.DataSource = ds.Tables[0];
19 this.GridView1.DataKeyNames = new string[] { "CustomerID" };
20 this.GridView1.DataBind();
21 }
22 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
23 {
24 string strKeys = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
25 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
26 SqlCommand cmd = new SqlCommand();
27 cmd.CommandText = "Delete from Customers where CustomerID=@CustomerID";
28 cmd.Connection = con;
29 SqlParameter sp = new SqlParameter("@CustomerID",SqlDbType.NChar,5);
30 sp.Value = strKeys;
31 cmd.Parameters.Add(sp);
32 if (con.State == ConnectionState.Closed)
33 {
34 con.Open();
35 }
36 cmd.ExecuteNonQuery();
37
38 con.Close();
39 databind();
40 }
41 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
42 {
43 if (e.Row.RowType == DataControlRowType.DataRow)
44 {
45 LinkButton lb = (LinkButton)e.Row.Cells[5].Controls[0];
46 lb.Attributes.Add("onclick", "return confirm('确认删除吗?');");
47 }
48 }
49 protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
50 {
51 for (int i = 0; i < this.GridView1.Rows.Count; i++)
52 {
53 CheckBox cb = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
54 cb.Checked = this.CheckBox2.Checked;
55
56 }
57
58 }
59 protected void Button1_Click(object sender, EventArgs e)
60 {
61 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
62 SqlCommand cmd = new SqlCommand();
63 cmd.Connection = con;
64 for (int i = 0; i < this.GridView1.Rows.Count; i++)
65 {
66 CheckBox cb = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
67 if (cb.Checked)
68 {
69 cmd.CommandText = "Delete From Customers where CustomerID =@CustomerID";
70 SqlParameter sp = new SqlParameter("@CustomerID", SqlDbType.NChar, 5);
71 sp.Value = this.GridView1.DataKeys[i].Value.ToString();
72 cmd.Parameters.Add(sp);
73 if (con.State == ConnectionState.Closed)
74 {
75 con.Open();
76 }
77 cmd.ExecuteNonQuery();
78 cmd.Parameters.Clear();
79 con.Close();
80
81 }
82 }
83 databind();
84 }
相关阅读 更多 +