【问题标题】:Unable to check all check boxes in a GridView无法选中 GridView 中的所有复选框
【发布时间】:2019-09-04 16:19:06
【问题描述】:

当我单击复选框时,我正在使用网格视图复选框来选择网格视图中的所有值,但我面临的问题是它选择了唯一的第一页值我如何编码把所有的价值观都带进来,但在设计中却行不通

这是图片

当我按下所有检查按钮时,我希望所有复选框都在设计中检查。

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

【问题讨论】:

  • 那么代码在哪里可以帮助你?

标签: c# gridview checkbox preview


【解决方案1】:

您可以在代码中保留一个布尔字段,并在单击全选时将其值设置为true。加载新页面时,您可以选中该字段以自动显示所有已选中。导出网格时也可以这样做。

【讨论】:

    【解决方案2】:

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

    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;
                }
            }
        }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2016-06-05
        • 1970-01-01
        • 1970-01-01
        • 2012-04-10
        • 2017-03-18
        • 2016-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多