By default, multiple
CheckBoxes in
GridView are meant for
multiple selection, thus in order to make it work as
single selection i.e. only one (single)
CheckBox can be selected at a time,
JavaScript needs to be used to achieve the same.
HTML Markup
The
HTML Markup consists of:
GridView - For displaying data.
Columns
The
GridView consists of one
TemplateField column and Three
BoundField columns.
TemplateField - The TemplateField column consists of ItemTemplate.
ItemTemplate – It consists of a
CheckBox.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Customer Id" HeaderStyle-Width="80" />
<asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Width="120" />
<asp:BoundField DataField="Country" HeaderText="Country" HeaderStyle-Width="120" />
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding the ASP.Net GridView control
Inside the
Page_Load event handler, an object of
DataTable is created.
Then, three columns are added to the
DataTable Columns collection using the
AddRange method.
Once the schema is ready i.e. all the columns are defined, some rows have been added using the Rows.Add method.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"),
New DataColumn("Name"),
New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
JavaScript function to select one (single) CheckBox from multiple CheckBoxes in GridView
Inside the
JavaScript window
onload event handler, first the
GridView is referenced and then all the
CheckBoxes inside it are referenced.
Then a loop is executed over the CheckBoxes and Click event handler is attached.
Inside the
Click event handler, a check is made to make sure whether the clicked
CheckBox is
checked and if it is
checked then all the other
CheckBoxes are
unchecked.
<script type="text/javascript">
window.onload = function () {
var grid = document.getElementById("<%= gvCustomers.ClientID%>");
var chks = grid.getElementsByTagName("INPUT");
for (var i = 0; i < chks.length; i++) {
if (chks[i].type == "checkbox") {
chks[i].onclick = function () {
for (var i = 0; i < chks.length; i++) {
if (chks[i] != this && this.checked) {
chks[i].checked = false;
}
}
};
}
}
};
</script>
Screenshot
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Demo
Downloads