用户要求对DATAGRID中的数据经过查询后进行批量删除,也就是满足条件查询出来的数据要全部删除. 我在SQL SERVER的存储过程中做了个 DELETE 产品信息 FROM where 产品id=@material_id.  
    DATAGRID中有产品ID, 供应商等等, 产品ID是隐藏的, 我想做个DELETE按钮, 在所有数据查询出来时,按DELETE把表中数据删除!   现在碰到的问题是: 如何取到所删除项的ID号,也就是我需要的@material_id.   

解决方案 »

  1.   

    DataGrid的绑定肯定是有数据源的,你可以数据源中的产品ID来作为条件啊
      

  2.   

    将ID绑定到DataGrid中(通过模板),然后应常该列就OK了,用户点击的时候获取当前行,然后可以取出ID。
      

  3.   

    我是邦定ID的,获取 当前行的ID是可以的, 但我要批量 删除的话, 我就不知道怎么得到多个的ID
      

  4.   

    private void dgrdMaterialInfo_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    Response.Redirect("DelMaterial.aspx?id="+e.Item.Cells[0].Text);
    }
    这个是我做的单个删除, 这里ID号是当前行, 如果要多个删除的话,是不是要做个循环?    要是改为 用CHECKBOX的方式,是不是比较可行点?
      

  5.   

    http://community.csdn.net/Expert/TopicView3.asp?id=4230707
      

  6.   

    如果你要把dg里面的数据都删除,那么你的dg有没有分页呢?你按delete按钮,要把dg里面的数据全部删除,还是dg前面有个checkbox,把checked的纪录删除呢?
    1。如果你dg没有checkbox,也有分页。那么你批量删除必须使用数据源,从数据源获取ID,传去sql,批量删除。
    2。如果有checkbox,可以先循环取得选中的ID,再批量删除。你先决定工作方式,再考虑方法。
      

  7.   

    我现在做的是CHECHBOX的形式,这是"删除" 按钮代码. (我现在只能选择一条,删一条,不能多条删)
    private void btdel_Click(object sender, System.EventArgs e)
    {
    for(int i=0;i<dgrdMaterialInfo.Items.Count;i++)
    {
    CheckBox mychk = (CheckBox)dgrdMaterialInfo.Items[i].FindControl("chkExport");
    if(mychk.Checked == true)
    {
    Response.Redirect("DelMaterial.aspx?id="+dgrdMaterialInfo.Items[i].Cells[i].Text);

    } }
    }这是连接到下一页的确认页(DelMateial.aspx)的代码:
    private void btnOk_Click(object sender, System.EventArgs e)
    {
    IQuotationMaterialDAL iqmd=new QuotationMaterialDAL(); iqmd.Delete(int.Parse(Request["id"].ToString()),0); Response.Redirect("QuotationMaterial.aspx");
    }
    这是QuotationMaterialDAl 中DELETE的代码
    public void Delete(int id,int target)
    {
    SqlDbProxy sdp=new SqlDbProxy("sp_add_del_material");
    sdp.AddParameter("@action","1");
    sdp.AddParameter("@material_id",id);
    sdp.AddParameter("@target",target); sdp.ExecuteNonQuery();
    sdp.Close();
    }
      

  8.   

    你的效率有点低了吧! DELETE 产品信息 FROM where 产品id=@material_id
    这样只能一条一条删除,你可以改成
     DELETE 产品信息 FROM where 产品id in @Condition当你查询出记录时,把查询条件记录一下(或者通过循环把ID取出拼串),然后传入进行删除操作,这样效率会高很多啊!
      

  9.   

    page_load中:
    if(!IsPostBack)
    {
    this.Button2.Attributes.Add("onclick","return(myCheck());");

    } 删除按钮中:
    SqlConnection MyConnection = new SqlConnection("server=(local);database=bysj;Trusted_Connection=yes");
    string DeleteCmd = "DELETE FROM ygwage WHERE ygid = @ygid AND wagedate BETWEEN @LowerDate AND @UpperDate";
    //And dixin=@dixin";
    SqlCommand MyCommand = new SqlCommand(DeleteCmd, MyConnection);
    MyCommand.Parameters.Add("@ygid", SqlDbType.Int);

    MyCommand.Parameters.Add(new SqlParameter("@LowerDate", SqlDbType.DateTime));
    MyCommand.Parameters["@LowerDate"].Value = Convert.ToDateTime(TextBox2.Text);

    MyCommand.Parameters.Add(new SqlParameter("@UpperDate", SqlDbType.DateTime));
    MyCommand.Parameters["@UpperDate"].Value = Convert.ToDateTime(TextBox3.Text);

                 bool IsChecked = false; 
    // 此回圈判断哪些核取方块已被勾选,并将相对应的资料记录删除。
    foreach (DataGridItem CheckBoxItem in DataGrid1.Items)
    {   
    IsChecked = ((CheckBox)CheckBoxItem.FindControl("CB")).Checked;
    // if(IsChecked==false)
    // {
    // Page.RegisterStartupScript("Alert","<script language=javascript>alert('请选择要删除的项!');</script>");
    // }
    if (IsChecked) 
    {
    // 取得使用者欲删除之资料记录的主索引键(亦即员工编号)
    // 然後将它指派给 DELETE 命令叙述中的参数。
    MyCommand.Parameters["@ygid"].Value =DataGrid1.DataKeys[CheckBoxItem.ItemIndex];
    //MyCommand.Parameters["@dixin"].Value =DataGrid1.DataKeys[CheckBoxItem.ItemIndex];
    MyCommand.Connection.Open();
    try
    {
    // 呼叫 ExecuteNonQuery() 方法以便针对资料来源执行 DELETE 命令

    MyCommand.ExecuteNonQuery();
    Message1.Text = "成功删除";
    Message1.Visible=true;
    //Page.RegisterStartupScript("Alert","<script language=javascript>alert('成功删除!');</script>");
    }
    catch 
    //(SqlException)
    {
    Message.Text = "错误: 无法删除资料纪录";
    //Response.Write("<script>alert('删除失败!');history.back();</Script>");
    }
    MyCommand.Connection.Close();
    }
    }
    // 重新系结至资料来源
    BindGridToSource();
    }
    前台JS:<script language="javascript">
    function myCheck()
                  {
        var mycount = 0 ;
    var mm = document.getElementsByTagName("input").length ;
    for(var i=0;i<mm;i++)
    {
    var dd = document.getElementsByTagName("input").item(i);
    if(dd.type == "checkbox")
    {
    if(dd.checked == true)
    {
    mycount += 1;
    }
    }
    } if(mycount == 0)
    {
    alert("您还未选择,请选择!");
    return(false);
    }
    else
    {
    return(confirm("本次操作将删除该时间段的所有记录,注意!是该时间段!您真的要删除它吗?"));
    }
    }
    </script>
    全选:
    <script language="javascript">
    <!--
    function CheckAllCus()
    {     
    for (var k=0;k<document.Form1.elements.length;k++)
    {
    var e = document.Form1.elements[k];
    if (e.id!= 'checkAccept')
     
    e.checked = document.Form1.checkAccept.checked;
    }
    }
    //-->
    </script>
    <script language="JavaScript">全选调用:
    <asp:TemplateColumn>
    <HeaderStyle Width="3%"></HeaderStyle>
    <HeaderTemplate>
    <INPUT id="checkAccept" type="checkbox" onclick="CheckAllCus()">
    <asp:Label id="Label4" runat="server">全选</asp:Label>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox id="CB" runat="server"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    希望对你有帮助:)
      

  10.   

    如何通过循环把ID取出拼串?      我现在就只能得到当前行的ID,  
    for(int i=0;i<dgrdMaterialInfo.Items.Count;i++)
    {
    CheckBox mychk = (CheckBox)dgrdMaterialInfo.Items[i].FindControl("chkExport");
    if(mychk.Checked == true)
    {
    Response.Redirect("DelAllMaterial.aspx?id="+dgrdMaterialInfo.Items[i].Cells[0].Text);

    }
      

  11.   

    你想实现怎样的删除呢?是把所有选中的checkbox的内容删除掉的话,我上面的那种就能实现啊
    你选几个就删几个啊,全选就删除全部的咯,
      

  12.   

    你做的思路和我不大一样,我有点看不懂!   想实现的结果可能是一样的,是把所有选中的checkbox的内容删除掉.
      

  13.   

    重新查询出数据源。把所有的ID都排成字符串。BookCourseCollection bookcourse=BookCourseManager.GetList("","",CourseID,"");
    foreach(BookCourse bo in bookcourse)
    {
    LabCourseBook.Text= "("+ bo.BookCode+")"+bo.BookName;
    CourseBookCount+=LabCourseBook.Text + " , ";
    }
    if(bookcourse.Count==0)
    {
    LabCourseBook.Text="&nbsp;";
    }
    else
    {
    LabCourseBook.Text= CourseBookCount.Substring(0,CourseBookCount.Length-2) ; 
    }
      

  14.   

    http://blog.csdn.net/kingdhy/archive/2006/03/06/616502.aspx