Selecting Checkboxes inside the GridView Control using ASP.Net

HTML CODE: Just copy and paste the following code in your .ASPX page.

<form id=”form1″ runat=”server”>
<table width=”90%”>
<td colspan=”4″>
<asp:GridView ID=”DataGV” runat=”server” AutoGenerateColumns=”False” CellPadding=”4″
GridLines=”Horizontal” BackColor=”White” BorderColor=”#336666″ BorderStyle=”Double”
BorderWidth=”3px” Font-Bold=”True” Font-Names=”Verdana” Font-Size=”X-Small” Height=”1px”
Width=”100%” AllowSorting=”True” HorizontalAlign=”Center” DataKeyNames=”EmployeeID”
AllowPaging=”True” PageSize=”50″>
<RowStyle BackColor=”White” ForeColor=”#333333″ />
<asp:TemplateField HeaderText=”Select”>
<asp:CheckBox ID=”CheckBox1″ runat=”server” />
<asp:Label ID=”LBLEmpID” runat=”server” Text='<%# container.dataitem(“EmployeeID”) %>’
<HeaderStyle HorizontalAlign=”Left” />
<FooterStyle HorizontalAlign=”Left” />
<asp:TemplateField HeaderText=”Name” SortExpression=”LastName”>
<HeaderStyle HorizontalAlign=”Left” />
<asp:Label ID=”LblName” runat=”server” Text='<%# trim(Eval (“LastName”)) + “, ” + trim( Eval (“FirstName”))%>’></asp:Label>
<asp:BoundField DataField=”Title” HeaderText=”Title” SortExpression=”Title”>
<HeaderStyle HorizontalAlign=”Left” />
<asp:BoundField DataField=”City” HeaderText=”City” SortExpression=”City”>
<HeaderStyle HorizontalAlign=”Left” />
<asp:TemplateField HeaderText=”HireDate” SortExpression=”HireDate”>
<ItemStyle HorizontalAlign=”Right” />
<HeaderStyle HorizontalAlign=”Right” />
<asp:Label ID=”LblHireDate” runat=”server” Text='<%# Bind(“HireDate”, “{0:d}”) %>’></asp:Label>
<HeaderStyle BackColor=”White” Font-Bold=”True” ForeColor=”Black” Font-Size=”Small”
VerticalAlign=”Top” />
<AlternatingRowStyle BackColor=”#D6E3F7″ />
<FooterStyle BackColor=”#009900″ Font-Bold=”True” ForeColor=”White” Font-Size=”Small”
HorizontalAlign=”Right” />
<PagerSettings PageButtonCount=”30″ Mode=”NumericFirstLast” NextPageText=”” PreviousPageText=””
Position=”TopAndBottom” />
<PagerStyle BackColor=”Silver” ForeColor=”White” HorizontalAlign=”Center” Font-Names=”Verdana”
Font-Size=”Small” />
<td align=”center” colspan=”4″>
<asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” Width=”129px” /></td>
<td colspan=”4″>

CODE BEHIND: Just copy and paste the following code in your .ASPX.vb page.

‘Copy and paste the following code in your page_Load event:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
‘Dim objbllNews As New BLL.News
If (IsPostBack = False) Then
Dim dsNews As DataSet
Dim dsData As DataSet = New DataSet()
Dim CMD As SqlCommand

‘Change the following connection string depends on your sql server ‘authentication
Dim con As SqlConnection = New SqlConnection(“Data Source=localhost;Initial Catalog=Northwind;TimeOut=60;User ID=sa;Password=;”)
CMD = New SqlCommand(“SELECT * FROM [Employees]”, con)

Dim adpt As New SqlDataAdapter(CMD)

DataGV.DataSource = dsData
Catch ex As Exception
Throw ex
End Try
End If
End Sub

‘Write the following code in your GridView’s RowDataBound event:

Protected Sub DataGV_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles DataGV.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then ‘1
Dim Chk As CheckBox = e.Row.FindControl(“CheckBox1″) ‘2
Page.ClientScript.RegisterStartupScript(Me.GetType(), “MyScript”, ” function HighlightSelected(colorcheckboxselected, RowState) { if (colorcheckboxselected.checked)’#FFAA63′; else { if (RowState==’0′)’white'; else’#D6E3F7′; } }”, True) ‘3
Chk.Attributes.Add(“onclick”, “HighlightSelected(this,'” + Convert.ToString(e.Row.RowState) + “‘ );”) ‘4
End If ‘5
End Sub

‘Write the following code in your button’s click event

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If IsPostBack = True Then
Dim str As StringBuilder = New StringBuilder()
‘ Select the checkboxes from the GridView control
Dim i As Integer
For i = 0 To DataGV.Rows.Count – 1 Step i + 1
Dim row As GridViewRow = DataGV.Rows(i)
Dim isChecked As Boolean = (CType(row.FindControl(“CheckBox1″), CheckBox)).Checked
If (isChecked) Then
End If
‘ prints out the result
End If
End Sub



any query Contact: [email protected]