使用复选框选择gridview中的所有行,单击按钮时删除

时间:2013-11-20 18:48:46

标签: c# asp.net

我在网格视图标题中有一个ckeck框字段。选中此复选框后的所有复选框 在网格视图中进行检查。现在我想删除按钮上的所有行。

我的aspx页面中的复选框代码如下:

 <HeaderTemplate>
       Select All: <asp:CheckBox ID="chkboxSelectAll"  AutoPostBack="true" OnCheckedChanged="chkboxSelectAll_CheckedChanged" 
       runat="server"/>
           </HeaderTemplate>
            <ItemTemplate>
            <asp:CheckBox ID="chkEmp" runat="server"></asp:CheckBox>
            </ItemTemplate></asp:TemplateField>

在我的代码背后,我尝试了这个,但它不起作用: 另外在我的网格视图中,DataKeyNames =“id”和bindgrid()方法工作正常。

用于选择所有行:

 protected void chkboxSelectAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox ChkBoxHeader = (CheckBox)Grd.HeaderRow.FindControl("chkboxSelectAll");
        foreach (GridViewRow row in Grd.Rows)
        {
            CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkEmp");
            if (ChkBoxHeader.Checked == true)
            {
                ChkBoxRows.Checked = true;
               }
} 

删除所有选定的行

 protected void btn_click(object sender, EventArgs e)
{
    CheckBox ChkBoxHeader = (CheckBox)Grd.HeaderRow.FindControl("chkboxSelectAll");

    foreach (GridViewRow row in Grd.Rows)
    {
        // Only look in data rows, ignore header and footer rows
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkEmp");

            if (ChkBoxHeader.Checked == true)
            {
                ChkBoxRows.Checked = true;

                var id = Grd.DataKeys[row.RowIndex].Value;
                SqlConnection con = new SqlConnection(constr);
                string qry = "delete from empdetail where id=@id";
                SqlCommand cmd = new SqlCommand(qry, con);
                cmd.Parameters.AddWithValue("@id", id);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                bindgrid();
            }

        }
    }
}

请帮帮我。我得到的错误是“索引超出范围。必须是非负的并且小于集合的大小。 参数名称:索引“

2 个答案:

答案 0 :(得分:2)

您的第一个问题是您正在搜索chkEmp复选框,但它在标题行中不存在,因为foreach (GridViewRow row in Grd.Rows)将遍历网格中的所有行(包括标题,数据)和页脚行)。

网格视图标记中的ItemTemplate适用于DataRow类型的行,因此您需要将chkEmp的搜索限制为仅数据行,如下所示:

protected void chkboxSelectAll_CheckedChanged(object sender, EventArgs e)
{
    CheckBox ChkBoxHeader = (CheckBox)Grd.HeaderRow.FindControl("chkboxSelectAll");

    foreach (GridViewRow row in Grd.Rows)
    {
        // Only look in data rows, ignore header and footer rows
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkEmp");

            if (ChkBoxHeader.Checked == true)
            {
                ChkBoxRows.Checked = true;

                var id = Grd.DataKeys[row.RowIndex].Value;
                SqlConnection con = new SqlConnection(constr);
                string qry = "delete from empdetail where id=@id";
                SqlCommand cmd = new SqlCommand(qry, con);
                cmd.Parameters.AddWithValue("@id", id);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                bindgrid();
            }
            else
            {
                ChkBoxRows.Checked = false;
            }
        }
    }
}

答案 1 :(得分:0)

     <asp:GridView ID="GrdAtt" runat="server" CssClass="table table-small-font table-bordered table-striped" Font-Size="Small" EmptyDataRowStyle-ForeColor="#cc0000" HeaderStyle-Font-Size="10" HeaderStyle-Font-Names="Arial" HeaderStyle-Font-Italic="true"
                                                                                AutoGenerateColumns="False" EmptyDataText="No Data Found" OnRowDataBound="GrdEmplistFromAtt_RowDataBound"
                                                                                HeaderStyle-ForeColor="#990000">
                                                                                <Columns>
                                                                                    <asp:TemplateField HeaderText=" " HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"
                                                                                        ItemStyle-Width="25px">
                                                                                        <HeaderTemplate>
                                                                                            <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkAll_CheckedChanged" />
                                                                                        </HeaderTemplate>
                                                                                        <ItemTemplate>
                                                                                            <asp:CheckBox ID="chkSingle" runat="server" />
                                                                                        </ItemTemplate>
                                                                                        <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                                                                                        <ItemStyle HorizontalAlign="Center" Width="10px"></ItemStyle>
                                                                                    </asp:TemplateField>
 </Columns>
                                                                                <HeaderStyle HorizontalAlign="Justify" VerticalAlign="Top"
                                                                                    Font-Bold="true" />
                                                                                <RowStyle Font-Size="Small" Height="1" Font-Italic="true" />
                                                                            </asp:GridView>






protected void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk_All = (CheckBox)GrdAtt.HeaderRow.FindControl("chkAll");
            if (chk_All.Checked == true)
            {
                foreach (GridViewRow gvr in GrdEmplistFromAtt.Rows)
                {
                    CheckBox chk_Single = (CheckBox)gvr.FindControl("chkSingle");
    
                    if (chk_Single.Visible == true)
                    {
                        chk_Single.Checked = true;
                        lblSelectedRecord.InnerText = (Convert.ToInt32(lblSelectedRecord.InnerText) + 1).ToString();
                    }
                }
            }
            else
            {
                foreach (GridViewRow gvr in GrdEmplistFromAtt.Rows)
                {
                    CheckBox chk_Single = (CheckBox)gvr.FindControl("chkSingle");
                    chk_Single.Checked = false;
                    lblSelectedRecord.InnerText = "0";
                }
            }
        }