If you want to check a checkbox in a particular row, find the CheckBox you want to check using FindControl method of the that Row and set its checked property to True.
Protected Sub Button1_Click(sender As Object, e As System.EventArgs)
Dim cb As CheckBox = CType(GridView1.Rows(0).FindControl("CheckBox1"), CheckBox)
cb.Checked = True
End Sub
The above code checks the CheckBox in the first row of the gridview. If you want to check CheckBoxes in all the rows you can do it in RowDataBound event
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cb As CheckBox = CType(e.Row.FindControl("CheckBox1"), CheckBox)
cb.Checked = True
End If
End Sub
Please note: "CheckBox1" is the ID of my check box in ItemTemplate
Kindly mark this post as "Answer", if it helped you.
(Talk less..Work more)
Marked as answer by veeves on May 01, 2012 06:36 PM
basheerkal
Star
10672 Points
2426 Posts
Re: Set checkbox to true in gridview
May 01, 2012 02:17 AM|LINK
If you want to check a checkbox in a particular row, find the CheckBox you want to check using FindControl method of the that Row and set its checked property to True.
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Dim cb As CheckBox = CType(GridView1.Rows(0).FindControl("CheckBox1"), CheckBox) cb.Checked = True End SubThe above code checks the CheckBox in the first row of the gridview. If you want to check CheckBoxes in all the rows you can do it in RowDataBound event
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim cb As CheckBox = CType(e.Row.FindControl("CheckBox1"), CheckBox) cb.Checked = True End If End SubPlease note: "CheckBox1" is the ID of my check box in ItemTemplate
(Talk less..Work more)