如何在gridview中获取复选框的值?

时间:2012-02-15 13:19:53

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

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

当用户检查并按下按钮时 这就是我的网格外观

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

        }

任何人都可以帮助我......

4 个答案:

答案 0 :(得分:1)

您可以使用SelectedIndex属性获取当前选定的行,然后从该行获取所需的单元格。类似的东西:

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

答案 1 :(得分:1)

假设您使用TemplateFieldCheckBox ID="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查询已经存在,因此您不需要额外的行循环。

答案 2 :(得分:1)

我解决了我的答案......

这是代码,我希望它可能对某人有帮助....

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


        }
    }

答案 3 :(得分:0)

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