无法选中GridView中的所有复选框

时间:2019-04-14 14:29:47

标签: c# gridview checkbox preview

enter image description here,当我单击复选框时,我正在使用“网格视图”复选框来选择网格视图中的所有值,但是我面临的问题是,无论如何我都只能选择首页值已经进行了编码以将所有值都包含在内,但在设计中却无法解决

这是图片

enter image description here

当我按下全部复选按钮时,我希望所有复选框都已选中。

 protected void gvBatch_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer && e.Row.RowType != DataControlRowType.Pager)
            {
                DropDownList ddlcountry1 = (DropDownList)e.Row.FindControl("ddlcountry");
                populateLocationValues(ddlcountry1);
                {
                    ArrayList checkboxvalues = (ArrayList)Session["BP_PrdId"];
                    //string Bp_Id = "";
                    if (checkboxvalues != null && checkboxvalues.Count > 0)
                    {

                        string strBp_Id = ((HiddenField)e.Row.FindControl("hf_ProductLblId")).Value.ToString();
                        if (checkboxvalues.Contains(strBp_Id))
                        {
                            CheckBox myCheckBox = (CheckBox)e.Row.FindControl("chkPLPSltItem");
                            myCheckBox.Checked = true;
                        }
                    }
                }
                DataSet dsaccess = MenuRestriction();
                DataRow dr = null;
                string sView = "";
                string sEdit = "";
                string sInsert = "";
                string sDeactive = "";

                if (dsaccess.Tables.Count > 0)
                {
                    if (dsaccess.Tables[0].Rows.Count > 0)
                    {
                        dr = dsaccess.Tables[0].Rows[0];
                        sView = dr["MnuRgts_View"].ToString();
                        sEdit = dr["MnuRgts_Edit"].ToString();
                        sInsert = dr["MnuRgts_Insert"].ToString();
                        sDeactive = dr["MnuRgts_DeActivate"].ToString();

                        if (sInsert == "Y" && sDeactive == "Y")
                        {
                            BtnDelete.Visible = true;
                            imgNew.Visible = true;
                        }
                        else
                        {
                            BtnDelete.Visible = false;
                            imgNew.Visible = false;

                            if (sInsert == "Y")
                            {
                                imgNew.Visible = true;
                            }
                            if (sDeactive == "Y")
                            {
                                BtnDelete.Visible = true;
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            log.Error("gvBatch_RowDataBound", ex);
        }
    }
  protected void gvBatch_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            RememberOldValues();
            gvBatch.PageIndex = e.NewPageIndex;
            //RetrieveValues();
            BindGrid();
            LoadLocation();
            //RePopulateValues();
        }
   private void RememberOldValues()
    {
        ArrayList checkboxvalues = new ArrayList();
        string strBp_Id = "";
        foreach (GridViewRow row in gvBatch.Rows)
        {
            //index = (int)gvBatch.DataKeys[row.RowIndex].Value;
            strBp_Id = ((HiddenField)row.FindControl("hf_ProductLblId")).Value.ToString();
            bool result = ((CheckBox)row.FindControl("chkPLPSltItem")).Checked;

            // Check in the Session
            if (Session["BP_PrdId"] != null)
                checkboxvalues = (ArrayList)Session["BP_PrdId"];
            if (result)
            {
                if (!checkboxvalues.Contains(strBp_Id))
                    checkboxvalues.Add(strBp_Id);
            }
            else
            {
                if (checkboxvalues.Contains(strBp_Id))
                    checkboxvalues.Remove(strBp_Id);
            }
        }
        if (checkboxvalues != null && checkboxvalues.Count > 0)
            Session["BP_PrdId"] = checkboxvalues;
    }
protected void gvBatch_PreRender(object sender, EventArgs e)
    {
        try
        {
            if (gvBatch.TopPagerRow != null)
            {
                ((Label)gvBatch.TopPagerRow.FindControl("lbCurrentPage")).Text = (gvBatch.PageIndex + 1).ToString();
                ((Label)gvBatch.TopPagerRow.FindControl("lbTotalPages")).Text = gvBatch.PageCount.ToString();

                ((LinkButton)gvBatch.TopPagerRow.FindControl("lbtnFirst")).Visible = gvBatch.PageIndex != 0;
                ((LinkButton)gvBatch.TopPagerRow.FindControl("lbtnPrev")).Visible = gvBatch.PageIndex != 0;
                ((LinkButton)gvBatch.TopPagerRow.FindControl("lbtnNext")).Visible = gvBatch.PageCount != (gvBatch.PageIndex + 1);
                ((LinkButton)gvBatch.TopPagerRow.FindControl("lbtnLast")).Visible = gvBatch.PageCount != (gvBatch.PageIndex + 1);

                DropDownList ddlist = (DropDownList)gvBatch.TopPagerRow.FindControl("ddlPageItems");
                ddlist.SelectedIndex = ddlist.Items.IndexOf(ddlist.Items.FindByValue(ViewState["DropDownPageItems"].ToString()));
                gvBatch.AllowPaging = true;
                gvBatch.TopPagerRow.Visible = true;
            }
        }
        catch (Exception ex)
        {

        }
    }
    protected void gvBatch_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "EDIT")
            {
                GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                string strAgentName = ((HiddenField)row.FindControl("hf_loginName")).Value.ToString();
                if (strAgentName != "")
                {
                    Response.Redirect("CustomerDetails.aspx?Name=" + strAgentName, false);
                }
            }
        }
        catch (Exception ex)
        {
            log.Error("gvAgentRowcommand_AgentSummary", ex);
        }
    }

3 个答案:

答案 0 :(得分:1)

您可以在代码中保留一个布尔字段,并在单击“全选”时将其值设置为true。加载新页面时,您可以选中该字段以自动显示所有选中的内容。导出网格时也可以执行相同的操作。

答案 1 :(得分:1)

您可以修改并使用以下方法

private void selectAllChecksInDAtaGrid()
    {
        foreach (DataGridViewRow item in myDataGrid.Rows)
        {
            if (Convert.ToBoolean(item.Cells["Column_Name"].Value) == false)
            {
                item.Cells["Column_Name"].Value = true;
            }
        }
    }

“ Column_name”是复选框列的名称。如果尚未命名,也可以使用索引号。 在您的情况下为0

private void selectAllChecksInDAtaGrid()
    {
        foreach (DataGridViewRow item in myDataGrid.Rows)
        {
            if (Convert.ToBoolean(item.Cells[0].Value) == false)
            {
                item.Cells[0].Value = true;
            }
        }
    }

答案 2 :(得分:0)

您应该更新(ArrayList)Session["BP_PrdId"]以包括gridview数据源的所有“ Id”。然后再次绑定数据。