【问题标题】:How to get the value in the gridview which the checkbox is checked?如何在选中复选框的gridview中获取值?
【发布时间】:2012-03-06 20:12:26
【问题描述】:

您好,我有一个带有复选框列的网格视图...

当用户检查并点击按钮时 这就是我的网格的样子

         checkboxColumn|ID|CustomerNAme
      --------------------------------------
                        1|asjd
                        2|asdfas
                        3|asdjka

                assign(Button)

当用户检查所需的行时,我只想获取网格的 ID 值并将其存储到数据库中

       protected void BtnAssignWork_Click(object sender, EventArgs e)
{

    for (int i = 0; i < GrdCustomerData.RowsInViewState.Count; i++)
    {
        GridDataControlFieldCell cell = GrdCustomerData.RowsInViewState[i].Cells[0] as GridDataControlFieldCell;
        CheckBox chk = cell.FindControl("Chkselect") as CheckBox;

        if (chk.Checked == true)
        {
            var allIDs = (from item in GrdCustomerData.Rows.Cast<GridRow>()                              
                          let id = int.Parse(item.Cells[1].Text)
                          where chk.Checked
                          select id).ToList();
            using (var myconn = new SqlConnection(connstring))
            {
                foreach (var id in allIDs)
                {
                    using (var mycomm = new SqlCommand("SP_AssignedWork", myconn))
                    {
                        myconn.Open();
                        mycomm.CommandType = CommandType.StoredProcedure;
                        mycomm.Parameters.Add("@LoanID", SqlDbType.VarChar).Value = id;
                        mycomm.Parameters.Add("@EmpID", SqlDbType.VarChar).Value = DDLSelectEmployee.SelectedValue;
                        mycomm.ExecuteNonQuery();
                        myconn.Close();
                    }
                }
            }

        }

谁能帮帮我....

【问题讨论】:

  • 我得到了控制,现在一切都很好......但是在插入时它会获取所有的 ids..它必须只获取选中列的 id。

标签: asp.net c#-4.0 gridview checkbox


【解决方案1】:

使用 Gridviewrows 类从该使用单元格 [1] 中查找当前行,该单元格表示该单元格中的控件,您可以从中获取 id

【讨论】:

    【解决方案2】:

    您可以使用SelectedIndex 属性来获取当前选定的行,然后从该行中获取您想要的单元格。比如:

     GridViewRow row = CustomersGridView.SelectedRow;
     int CustomerId = int.parse(row.Cells[2].Tex);
    

    【讨论】:

      【解决方案3】:

      假设您使用TemplateFieldCheckBoxID="Chkselect"

      protected void BtnAssignWork_Click(object sender, EventArgs e){
          var allIDs = (from item in GrdCustomerData.Rows.Cast<GridViewRow>()
                       let chk = (CheckBox)item.Cells[0].FindControl("Chkselect")
                       let id = int.Parse(item.Cells[1].Text)
                       where chk.Checked
                       select id).ToList();
      
          using(var myconn = new SqlConnection(connstring)){
               myconn.Open();
               foreach (var id in allIDs) { 
                  using(var mycomm = new SqlCommand("SP_AssignedWork", myconn)){
                      mycomm.CommandType = CommandType.StoredProcedure;
                      mycomm.Parameters.Add("@LoanID", SqlDbType.Int).Value = id;
                      mycomm.Parameters.Add("@EmpID", SqlDbType.Int).Value = int.Parse(DDLSelectEmployee.SelectedValue);
                      mycomm.ExecuteNonQuery();
                  }
               }
           }
       }
      

      编辑:为了澄清起见,我将这两个部分结合在一起。您不需要额外的行循环,因为 LINQ 查询已经这样做了。

      【讨论】:

      • 嗨...我编辑了我的答案你可以检查一下..我在这一行出现错误.." if (chkGrid.Checked == true)" 说对象引用未设置为 insatnace对象的
      • @Anil:我也编辑了我的答案,你看到了吗?除此之外,我无法告诉您更多信息,因为您没有显示 aspx 标记,但是您的按钮单击处理程序中的行循环对于 LINQ 查询是多余的(allIDs 已经包含所有选定的 ID)。
      • 获取相同的错误对象引用未设置为对象的实例
      • 我得到了控制,现在一切都很好....但是在插入时它会获取所有的 id..它必须只获取选中列的 id...
      • 您应该编辑您的原始问题并在那里添加您目前所拥有的内容。你用过什么代码?我的 LINQ 查询是否按预期工作?它应该只列出属于选中行的所有 ID。
      【解决方案4】:

      我解决了我的答案的任何方法....

      这是我希望对某些人有所帮助的代码......

          void grid1_RowDataBound(object sender, GridRowEventArgs e)
      {
          if (e.Row.RowType == GridRowType.DataRow && GrdCustomerData.RowsInViewState.Count > 0)
          {
              GridDataControlFieldCell cell = e.Row.Cells[0] as GridDataControlFieldCell;
              CheckBox chk = cell.FindControl("Chkselect") as CheckBox;
      
              GridDataControlFieldCell cellInViewState = GrdCustomerData.RowsInViewState[e.Row.RowIndex].Cells[0] as GridDataControlFieldCell;
              CheckBox chkInViewState = cellInViewState.FindControl("Chkselect") as CheckBox;
      
              if (cell.Value == chkInViewState.ToolTip)
              {
                  chk.Checked = chkInViewState.Checked;
              }
          }
      }
      
      protected void BtnAssignWork_Click(object sender, EventArgs e)
      {
          string LoanIds = "";
      
          for (int i = 0; i < GrdCustomerData.RowsInViewState.Count; i++)
          {
              GridDataControlFieldCell cell = GrdCustomerData.RowsInViewState[i].Cells[0] as GridDataControlFieldCell;
              CheckBox chk = cell.FindControl("Chkselect") as CheckBox;
      
              if (chk.Checked == true)
              {
                  if (!string.IsNullOrEmpty(LoanIds))
                     LoanIds += "";
      
                 LoanIds = chk.ToolTip;
                          SqlConnection myconn = new SqlConnection(connstring);
                          SqlCommand mycomm = new SqlCommand("SP_AssignedWork", myconn);
                          myconn.Open();
                          mycomm.CommandType = CommandType.StoredProcedure;
                          mycomm.Parameters.Add("@LoanID", SqlDbType.VarChar).Value = LoanIds;
                          mycomm.Parameters.Add("@EmpID", SqlDbType.VarChar).Value = DDLSelectEmployee.SelectedValue;
                          mycomm.ExecuteNonQuery();
                          myconn.Close();
                      }
                  }
      
      
              }
          }
      

      【讨论】:

      • 这是您问题的答案还是附加信息?如果是后者,则应将其包含在您的问题中。如果是前者,那么它是我的答案的副本,但应删除其他代码。我的答案中的代码对于您的BtnAssignWork_Click 来说是完整的。您再次迭代所有行这是没有意义的,因为 LINQ 查询和 foreach 已经这样做了。
      • 在你的回答中我无法找到复选框控件..在我的回答中我现在可以......你的回答是正确的,这就是我支持你的原因......任何方式我在你的回答中仍然有一个缺陷 var allids 正在占用所有 th ids...
      • 无论如何,您的代码没有意义,或者至少我的 LINQ 查询不再有意义。如果你想避免 LINQ,你可以简单地保持你的循环,通过id = int.Parse(item.Cells[1].Text) 获取 id。但是你应该删除 LINQ 和 foreach (你的外循环正在循环所有带有选中复选框的行)。唯一显然不起作用的是您在 Obout Grid 中获取对复选框的引用的方式。 (请参阅我编辑的答案,尤其是在 LINQ 查询中添加的Cells[0]
      猜你喜欢
      • 2012-09-23
      • 2017-01-16
      • 2014-07-14
      • 1970-01-01
      • 2018-06-15
      • 2017-11-03
      • 2013-10-25
      • 1970-01-01
      • 2011-03-02
      相关资源
      最近更新 更多