I have a gridview with a checkbox template column I need to set in vb code. I do not have a filed in the database to use as a source to populate this checkbox. Is there a way to set the value of a gridview checkbox in code?
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
veeves
Member
29 Points
11 Posts
Set checkbox to true in gridview
Apr 30, 2012 08:26 PM|LINK
I have a gridview with a checkbox template column I need to set in vb code. I do not have a filed in the database to use as a source to populate this checkbox. Is there a way to set the value of a gridview checkbox in code?
Veeves
mm10
Contributor
6445 Points
1187 Posts
Re: Set checkbox to true in gridview
Apr 30, 2012 10:21 PM|LINK
You can set the Checked attribute to true in either markup or code:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cb" runat="server" Checked="true" />
</ItemTemplate>
</asp:TemplateField>
bhaskar.mule
Contributor
2270 Points
659 Posts
Re: Set checkbox to true in gridview
May 01, 2012 01:51 AM|LINK
hi
ihope it will help you
http://csharpektroncmssql.blogspot.com/2011/12/check-box-in-grid-view-in-aspnet.html
Site:Rare technical solutions
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)
veeves
Member
29 Points
11 Posts
Re: Set checkbox to true in gridview
May 01, 2012 06:40 PM|LINK
The first example was what I needed. I am doing this outside of rowdatabound and rowcommand in a function I created.
Thanks