tangteng78
asked on
How to add checkbox programatically in Gridview?
Hi,
I'm able to add button programmatically, BUT not checkbox. Any ideas?
I'm able to add button programmatically, BUT not checkbox. Any ideas?
gvwInbox.AllowSorting = True
gvwInbox.AllowPaging = True
gvwInbox.PageSize = 10
Dim btnView1 As ButtonField = New ButtonField()
btnView1.ButtonType = ButtonType.Button
btnView1.Text = "View"
btnView1.CommandName = "View"
Dim chk As CheckBoxField = New CheckBoxField
chk.DataField = "subject"
'Add in web controls
gvwInbox.Columns.Add(btnView1)
gvwInbox.Columns.Add(chk)
gvwInbox.DataSource = objInbox.GetInbox(User.Identity.Name.ToString)
gvwInbox.DataBind()
Make sure the database field set using DataField property of the CheckBoxField is a boolean field rather than a textual one, e.g. Bit (SQL Server) or Yes/No (Access). I just tested it here and it works with boolean fields only.
You need to replace "Subject" with some other field like "IsCurrent" or whatever but a boolean field.
ASKER
The links doesn't provide the insight on how to add checkbox programatically.
However, i put in the checkbox in the itemtemplate. See below.
how do i access the "subject" value if i selected on certain checkbox on a click of a button (the button defined separate outside of the gridview)?
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
For Each row As GridViewRow In gvwInbox.Rows
Dim cb As CheckBox = row.FindControl("chk")
'I'm stuck here...do not know how to get the 'subject' value on specific checkbox checked.
Next
End Sub
However, i put in the checkbox in the itemtemplate. See below.
how do i access the "subject" value if i selected on certain checkbox on a click of a button (the button defined separate outside of the gridview)?
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
For Each row As GridViewRow In gvwInbox.Rows
Dim cb As CheckBox = row.FindControl("chk")
'I'm stuck here...do not know how to get the 'subject' value on specific checkbox checked.
Next
End Sub
<asp:GridView ID="gvwInbox" runat="server" AutoGenerateColumns="false" AllowSorting="true" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<%#Eval("subject")%>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<asp:CheckBox ID="chk" runat="server" /></td>
<td><%#Eval("subject")%></td>
</tr>
<tr>
<td>
<%#Eval("body")%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Please use the snippet below to get the checkbox, and then use its Checked property to evaluate if it is checked or not. Make sure if you use multiple ItemTemplates, pass the correct index position value into the Cells() collection.
Dim chkTemp As CheckBox = CType(row.Cells(0).FindControl("chk"), CheckBox)
ASKER
can you elaborate on this?
"Make sure if you use multiple ItemTemplates, pass the correct index position value into the Cells() collection."
"Make sure if you use multiple ItemTemplates, pass the correct index position value into the Cells() collection."
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.codeproject.com/KB/aspnet/datagridcheckboxcol.aspx