把断点设在这里,看有没有执行!
if(CB.Checked)
{
ID=newBook_DGrid.Items[i].Cells[1].Text;
oleDbDeleteCommand1.CommandText = "DELETE FROM NewBook where BookID ='"+ID+"'";
         oleDbDeleteCommand1.ExecuteNonQuery();
}

解决方案 »

  1.   

    试过了,没有执行。
    不知道是为什么应该出在CheckBox上吧。
    ???
      

  2.   

    这表示你的CB的赋值有问题
    CB=(CheckBox)(newBook_DGrid.Items[i].Cells[0].Controls[1]);试试CB=(CheckBox)(newBook_DGrid.Items[i].Cells[0].Controls[0]);
      

  3.   

    哦,错了
    Controls[0]是个textbox因该是CB=(CheckBox)(e.Items[i].Cells[0].Controls[1]);
      

  4.   

    阿,都写错了!sorry着急下班!
      

  5.   

    刚试完,代码因该没问题,唯一要改的地方是for(int i=0; i<row; i++)i从0开始这次看看能执行到if(CB.Checked)判断里面么!
      

  6.   

    CB=(CheckBox)(newBook_DGrid.Items[i].Cells[0].Controls[1]);
    改成:CB=(CheckBox)(newBook_DGrid.Items[i].FindControl("CheckBox1"));
      

  7.   

    是不是在第二次Page_Load时重新绑定了DataGrid?
      

  8.   

    好像不是。现在基本已经确定了,应该是checkbox上的问题,但不知具体应该是什么样子。有谁做过类似的功能吗?会不会是在html里面,checkbox的设置出问题了?
      

  9.   

    可能是你的Item出问题,你知道DataGrid有8种Item类型吗?你的checkbox是在ListItemType.Item类型和ListItemType.AlternatingItem类型中,你加上类型判断试试:if((newBook_DGrid.Items[i].ItemType==ListItemType.Item)||(newBook_DGrid.Items[i].ItemType==ListItemType.AlternatingItem))
    {
       ....
    }
      

  10.   

    使用request["check.."]..一个遍历循环,删除所有
      

  11.   

    那么请问这里应该是属于哪一种item呢?
      

  12.   

    调试一下不就知道了。
    在for(int i=1; i<row; i++)上面设置断点,然后一步步调试。
      

  13.   

    有这么夸张吗,这里有个例子。在页面上加入一个DataGrid和一个Button:
    <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:CheckBox Runat="server" ID="chk"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="Project_ID" HeaderText="ProjectID"></asp:BoundColumn>
    </Columns>
    </asp:DataGrid>
    <br>
    <asp:Button id="Button1" runat="server" Text="Delete"></asp:Button>下面是后台代码。
    Page_Load函数:
    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    SqlConnection conn = new SqlConnection("server=icyer;database=ChinaPackage;uid=sa;pwd=;");
    conn.Open();
    SqlDataAdapter da = new SqlDataAdapter("SELECT Project_ID FROM tbProject ORDER BY Project_ID", conn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    conn.Close();DataGrid1.DataSource = ds.Tables[0];
    DataGrid1.DataBind();
    }
    }Button的Click事件函数:
    private void Button1_Click(object sender, System.EventArgs e)
    {
    SqlConnection conn = new SqlConnection("server=icyer;database=ChinaPackage;uid=sa;pwd=;");
    conn.Open();
    SqlCommand cmd;
    int i;
    for (i = 0; i < DataGrid1.Items.Count; i++)
    {
    CheckBox chk = (CheckBox)DataGrid1.Items[i].FindControl("chk");
    if (chk.Checked == true)
    {
    string strCommand = "DELETE FROM tbProject WHERE Project_ID='" + DataGrid1.Items[i].Cells[1].Text + "'";
    cmd = new SqlCommand(strCommand, conn);
    cmd.ExecuteNonQuery();
    }
    }SqlDataAdapter da = new SqlDataAdapter("SELECT Project_ID FROM tbProject ORDER BY Project_ID", conn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    DataGrid1.DataSource = ds.Tables[0];
    DataGrid1.DataBind();
    }