vs2005中,用GridView来进行批量删除,如果有别的简便方法,更好!
谢谢!

解决方案 »

  1.   

    在GridView里面添加一个CheckBox控件列,配合外面全选/取消选择的CheckBox控件,在加上删除Button
      

  2.   

    已经添加上CheckBox,但是不知道如何得到他的字段,能不能代码提示下!
      

  3.   

    foreach (GridViewRow row in GridView1.Rows)
    {
       CheckBox cb = (CheckBox)row.FindControl("CheckBox1");   if (cb.Checked)
       {
           // 批量删除处理
       }
    }
      

  4.   

    <HTML>
    <HEAD>
    <title>searchwagemessage</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <LINK href="../../images/style.css" type="text/css" rel="stylesheet">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    <SCRIPT language="javascript" src="js\popup.js"></SCRIPT>
    <script language="javascript">
    function chkstr()

    if(document.all.TextBox2.value=='')
    {
     document.all.TextBox2.focus();
     alert('上限查询时间不能为空!');
     return false;
     }
     else if(document.all.TextBox3.value=='')
    {
     document.all.TextBox3.focus();
     alert('下限查询时间不能为空!');
     return false;
     }
     else if(document.all.TextBox1.value=='')
    {
     document.all.TextBox1.focus();
     alert('请输入查询条件!');
     return false;
     }
     }
    </script>
    <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">
    javascript:window.history.forward(1);
    </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <TABLE id="Table2" style="Z-INDEX: 104; LEFT: 24px; WIDTH: 603px; POSITION: absolute; TOP: 120px; HEIGHT: 168px"
    cellSpacing="1" cellPadding="1" width="603" border="1">
    <TR>
    <TD style="HEIGHT: 11px" align="center" bgColor="#0099cc"><FONT face="宋体"><FONT face="宋体"><STRONG><FONT face="宋体"><FONT face="宋体" color="#ff0033"><STRONG>员工工资查询</STRONG></FONT></FONT></STRONG></FONT></FONT></TD>
    </TR>
    <TR>
    <TD style="HEIGHT: 106px" align="center" colSpan="1" rowSpan="1"><asp:label id="Label2" runat="server" Font-Size="10" ForeColor="#400040">从</asp:label><asp:textbox id="TextBox2" runat="server" Width="80px" ReadOnly="True"></asp:textbox><A onclick="fPopUpDlg('js/date.htm', document.forms[0].TextBox2, 'winpop',300,100, 234, 261);return false"
    href="#"><IMG src="js\datetime.gif" border="0"></A>
    <asp:label id="Label3" runat="server" Font-Size="10" ForeColor="#400040">到 </asp:label><asp:textbox id="TextBox3" runat="server" Width="80px" ReadOnly="True"></asp:textbox><A onclick="fPopUpDlg('js/date.htm', document.forms[0].TextBox3, 'winpop',300,100, 234, 261);return false"
    href="#"><IMG src="js\datetime.gif" border="0"></A>
    <asp:label id="Label1" runat="server" Font-Size="10" ForeColor="#400040">员工编号</asp:label><asp:textbox id="TextBox1" runat="server" Width="84px"></asp:textbox><asp:button id="Button1" runat="server" Text="执行查询"></asp:button>
    <DIV style="OVERFLOW: auto; WIDTH: 594px; HEIGHT: 150px"><asp:datagrid id="DataGrid1" runat="server" Width="673px" HorizontalAlign="Center" ToolTip="双击查看详细信息"
    DataKeyField="ygid" CellPadding="3" BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#CCCCCC" AutoGenerateColumns="False">
    <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></SelectedItemStyle>
    <ItemStyle Font-Size="12px" ForeColor="#000066"></ItemStyle>
    <HeaderStyle Font-Size="12px" Font-Bold="True" HorizontalAlign="Center" ForeColor="White" BackColor="#006699"></HeaderStyle>
    <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
    <Columns>
    <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>
    <asp:BoundColumn DataField="ygid" HeaderText="员工编号">
    <HeaderStyle Font-Size="10pt" Width="5%"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="dixin" HeaderText="底薪/元">
    <HeaderStyle Font-Size="10pt" Width="5%"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="jlwage" HeaderText="奖金/元">
    <HeaderStyle Width="5%"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="chengfwage" HeaderText="惩罚金/元">
    <HeaderStyle Width="5%"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="sfwage" HeaderText="实发工资/元">
    <HeaderStyle Font-Size="10pt" Width="5%"></HeaderStyle>
    </asp:BoundColumn>
    <asp:BoundColumn DataField="wagedate" HeaderText="日期 " DataFormatString="{0:yyyy-MM-dd}">
    <HeaderStyle Font-Size="10pt" Width="5%"></HeaderStyle>
    </asp:BoundColumn>
    </Columns>
    <PagerStyle Visible="False" NextPageText="下一页" PrevPageText="上一页" HorizontalAlign="Left" ForeColor="#000066"
    BackColor="White" Mode="NumericPages"></PagerStyle>
    </asp:datagrid></DIV>
    </TD>
    </TR>
    <TR>
    <TD style="HEIGHT: 4px" vAlign="top"><FONT face="宋体"></FONT></TD>
    </TR>
    <TR>
    <TD style="HEIGHT: 3px" align="center" colSpan="1" rowSpan="1"><asp:button id="Button2" runat="server" Text="删除" Enabled="False"></asp:button><INPUT id="Reset1" type="reset" value="重置" name="Reset1" runat="server"></TD>
    </TR>
    <TR>
    <TD align="center"><FONT face="宋体"><webdiyer:aspnetpager id="AspNetPager1" runat="server" HorizontalAlign="Center" Wrap="False" TextBeforeInputBox="转到第"
    TextAfterInputBox="页" SubmitButtonText="GO" PageSize="6" ShowInputBox="Always" ShowCustomInfoSection="Left" alwaysShow="true"
    Font-Size="10"></webdiyer:aspnetpager></FONT></TD>
    </TR>
    </TABLE>
    <asp:label id="Message" style="Z-INDEX: 102; LEFT: 256px; POSITION: absolute; TOP: 80px" runat="server"
    Width="152px" Font-Size="15px" ForeColor="Red" Font-Bold="True"></asp:label><asp:label id="Message1" style="Z-INDEX: 103; LEFT: 136px; POSITION: absolute; TOP: 80px" runat="server"
    Width="88px" Font-Size="10pt" ForeColor="Red" Font-Bold="True"></asp:label>
    <asp:Button id="Button4" style="Z-INDEX: 105; LEFT: 56px; POSITION: absolute; TOP: 72px" runat="server"
    Text="Button"></asp:Button></form>
    </body>
    </HTML>
      

  5.   

    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();
      

  6.   

    vs2005: bool IsChecked = false;
                // 此回圈判断哪些核取方块已被勾选,并将相对应的资料记录删除。
                foreach (GridViewRow CheckBoxItem in gvRecyMsgList.Rows)
                {
                    IsChecked = ((CheckBox)CheckBoxItem.FindControl("CB")).Checked;
                    if (IsChecked)
                    {
                        // 取得使用者欲删除之资料记录的主索引键(亦即消息编号)
                        HiddenField hideMsg_no = (HiddenField)CheckBoxItem.Cells[1].FindControl("hideMsgno");
                        HiddenField hidselfdel = (HiddenField)CheckBoxItem.Cells[7].FindControl("hidSelfDel");
                        HiddenField hidrecdel = (HiddenField)CheckBoxItem.Cells[7].FindControl("hidReceieDel");
                        SqlParameter[] pamDel = { 
                            new SqlParameter("@intMsgno", SqlDbType.Int, 4),
                            new SqlParameter("@chrsendstate", SqlDbType.Char, 1),
                            new SqlParameter("@chrrecstate", SqlDbType.Char, 1),
                            new SqlParameter("@chrUserNo", SqlDbType.Char, 8)
                                                };
                        pamDel[0].Value = hideMsg_no.Value;
                        pamDel[1].Value = hidselfdel.Value;
                        pamDel[2].Value = hidrecdel.Value;
                        pamDel[3].Value = m_strStaffNo;  //用于删除接收箱中被删除到回收站的消息(因为在回收站中删除接收箱中的消息是针对msgRun表,而msgRun表中对于同一个Msg_No有很多人员,所以还原或删除时都得加上人员标志,不然某一个人去删除时会把其他接收人员的也会更新)
                        bool blnFlag = objDB.ExecuteProcedure("msgSendDelOrReceDel", pamDel);
                        if (!blnFlag)
                        {
                            WriteToLog("批量彻底删除消息:编号为:" + hideMsg_no.Value + "的消息时出错", "删除失败");
                            MessageBox.Show(this, "删除编号为" + hideMsg_no.Value + "的消息失败!");
                            return;
                        }
                    }
                }
                CalculateRecord();  //重新绑定记录数
                BindData();         //重新绑定消息条数
      

  7.   


    foreach (GridViewRow row in GridView1.Rows)
    {
       CheckBox chk = (CheckBox)row.FindControl("Check_Name");   if (chk.Checked)
       {
    ////////////
            }
    }