How to bind checkbox with gridview in asp.net 2.0
I take a gridview control and bind data dynamicaly using code.I taken backend tool as a Ms Access and now I want to add new column with checkbox so user can delete selected recored..So How can I bind checkbox with gridview...
You need to add a TemplateField to your GridView so that it generates a checkbox per row.
-
<asp:TemplateField>
-
<EditItemTemplate>
-
<asp:CheckBox ID="Chkbx_Selected" runat="server" />
-
</EditItemTemplate>
-
<ItemTemplate>
-
<asp:CheckBox ID="Chkbx_Selected" runat="server" />
-
</ItemTemplate>
-
</asp:TemplateField>
You can easily do this in your design view of your GridView.
When you have selected your GridView in design mode, you will see something that looks like a play button in the top right hand corner (a triangle).
Click it and a menu will appear.
From this menu click the Add Columns.
Select the CheckBoxField from the "Available Fields" section.
Click the Add button.
In the CheckBoxField properties, give the check box a name (like "Chkbx_Selected") so that you can find the control in your .NET code that is responsible for deleting the entry.
This will display check boxes next to each row in your GridView.
Now when you are performing the delete you need to loop through each row in your GridView, get the check box, and determine whether or not to delete that row...
eg
-
For Each dr In MyGridView.Rows
-
Dim chkbox as CheckBox = dr.FindControl("Chkbx_Selected")
-
If chkbox.Checked = True
-
'Delete the entry.
-
End If
-
Next
-
Cheers!
-Frinny