【问题标题】:select all rows in gridview using checkbox and delete on button click使用复选框选择gridview中的所有行并单击按钮删除
【发布时间】:2013-11-20 18:48:46
【问题描述】:

我在网格视图标题中有一个复选框字段。选中此复选框时,所有复选框 在网格视图中被检查。现在我想删除按钮单击时的所有行。

我的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();
            }

        }
    }
}

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

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您的第一个问题是您正在搜索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;
                }
            }
        }
    }
    

    【讨论】:

    • 解决方案是:我在 foreach 循环中使用了 bindgrid() 方法。把它放在循环之外可以解决问题。
    【解决方案2】:
         <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";
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 2013-02-22
      • 2013-06-08
      相关资源
      最近更新 更多