Gridview get Checkbox.Checked value

后端 未结 7 646
北荒
北荒 2020-12-17 10:23

I have a GridView that has 10 columns populated by CheckBoxes. But instead of using FindControl() is there a way to get the CheckBox.Checked value

相关标签:
7条回答
  • 2020-12-17 10:53

    Try this,

    Using foreach Loop:

    foreach (GridViewRow row in GridView1.Rows)
    {
         CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
         if (chk != null && chk.Checked)
         {
           // ...
         }
    }
    

    Use it in OnRowCommand event and get checked CheckBox value.

    GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
    int requisitionId = Convert.ToInt32(e.CommandArgument);
    CheckBox cbox = (CheckBox)row.Cells[3].Controls[0];
    
    0 讨论(0)
  • 2020-12-17 10:53

    You want an independent for loop for all the rows in grid view, then refer the below link

    http://nikhilsreeni.wordpress.com/asp-net/checkbox/

    Select all checkbox in Gridview

    CheckBox cb = default(CheckBox);
    for (int i = 0; i <= grdforumcomments.Rows.Count – 1; i++)
    {
        cb = (CheckBox)grdforumcomments.Rows[i].Cells[0].FindControl(“cbSel”);
    
        cb.Checked = ((CheckBox)sender).Checked;
    }
    
    Select checked rows to a dataset; For gridview multiple edit
    
    CheckBox cb = default(CheckBox);
    
    foreach (GridViewRow row in grdforumcomments.Rows)
    {
        cb = (CheckBox)row.FindControl("cbsel");
        if (cb.Checked)
        {
            drArticleCommentsUpdates = dtArticleCommentsUpdates.NewRow();
            drArticleCommentsUpdates["Id"] = dgItem.Cells[0].Text;
            drArticleCommentsUpdates["Date"] = System.DateTime.Now;dtArticleCommentsUpdates.Rows.Add(drArticleCommentsUpdates);
        }
    }
    
    0 讨论(0)
  • 2020-12-17 10:55

    For run all lines of GridView don't use for loop, use foreach loop like:

    foreach (GridViewRow row in yourGridName.Rows) //Running all lines of grid
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
             CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
    
             if (chkRow.Checked)
             {
                  //if checked do something
             }
        }
    }
    
    0 讨论(0)
  • 2020-12-17 10:57

    Blockquote

        foreach (GridViewRow row in tempGrid.Rows)
        {
            dt.Rows.Add();
            for (int i = 0; i < row.Controls.Count; i++)
            {
                Control control = row.Controls[i];
                if (control.Controls.Count==1)
                {
                    CheckBox chk = row.Cells[i].Controls[0] as CheckBox;
                    if (chk != null && chk.Checked)
                    {
                        dt.Rows[dt.Rows.Count - 1][i] = "True";
                    }
                    else
                    dt.Rows[dt.Rows.Count - 1][i] = "False";
                }
                else
                    dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text.Replace("&nbsp;", "");
            }
        }
    
    0 讨论(0)
  • 2020-12-17 10:59

    If you want a method other than findcontrol try the following:

     GridViewRow row = Gridview1.SelectedRow;
     int CustomerId = int.parse(row.Cells[0].Text);// to get the column value
     CheckBox checkbox1= row.Cells[0].Controls[0] as CheckBox; // you can access the controls like this
    
    0 讨论(0)
  • 2020-12-17 11:04
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chkbox = (CheckBox)row.FindControl("CheckBox1");
            if (chkbox.Checked == true)
            {
                // Your Code
            }
        }
    
    0 讨论(0)
提交回复
热议问题