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

问题描述 投票:0回答:3

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);
        }
    }
c# gridview checkbox preview
3个回答
1
投票

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


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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.