/////////////////
//注释内是我用的另一种方法,错误提示为集合中的元素可能已被更改不能继续遍历,也不成功.
//请问是为什么?第一中方法页面只是刷新了下,没有任何变化...
////////////////////
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;
using System.Text;public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        databind();
    }
    protected void dg_SelectedIndexChanged(object sender, EventArgs e)
    {    }
    public void databind()
    {
        string strconn = "server=(local);database=pubs;trusted_connection=true";
        SqlConnection conn = new SqlConnection(strconn);
        SqlDataAdapter da = new SqlDataAdapter("select * from jobs", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "jobs");
        dg.DataSource = ds.Tables[0].DefaultView;
        dg.DataBind();
    }
    protected void dg_EditCommand(object source, DataGridCommandEventArgs e)
    {
        dg.EditItemIndex = e.Item.ItemIndex;
        databind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CheckBox cb = new CheckBox();
        StringBuilder sb = new StringBuilder();
        int i, j = dg.Items.Count;
        for (i = 0; i < j; i++)
        {
            cb = (CheckBox)dg.Items[i].Cells[6].FindControl("CheckBox1");
            if (cb.Checked)
            {
                sb.Append(dg.Items[i].Cells[1].Text.Trim());
                sb.Append("','");
            }
        }
        string str = sb.ToString();        try
        {
            string str1 = "'" + str + "'";
            string strsql = "delete from jobs where job_id in" + "(" + str1 + ")";
            SqlConnection conn = new SqlConnection("server=(local);database=pubs;trusted_connection=true");
            SqlCommand comm = new SqlCommand(strsql, conn);
            try
            {
                conn.Open();
                comm.ExecuteNonQuery();            }
            catch (System.Exception eee)
            {
                throw new System.Exception(eee.Message);
            }
            finally
            {
                conn.Close();
            }        }
        catch (System.Exception er)
        {
            throw new Exception(er.Message);
        }
        /////////////////////////////////////////////////////////////////////////////
        //foreach (DataGridItem dgi in dg.Items)
        //{        //    CheckBox cb = (CheckBox)dgi.FindControl("CheckBox1");        //    if (cb.Checked)
        //    {
        //        string ID = dgi.Cells[1].Text;
        //        string strsql = "delete from jobs where job_id =" + ID;
        //        Label1.Text = strsql;
        //        SqlConnection conn = new SqlConnection("server=(local);database=pubs;trusted_connection=true");
        //        SqlCommand comm = new SqlCommand(strsql, conn);
        //        try
        //        {
        //            conn.Open();
        //            comm.ExecuteNonQuery();                 
                    
        //        }
        //        catch (System.Exception eee)
        //        {
        //            throw new System.Exception(eee.Message);
        //        }
        //        finally
        //        {
        //            conn.Close();
        //        }
        //    }
        /////////////////////////////////////////////////////////////////////////////////////
            databind();
        }    }