Selecting Checkboxes inside the GridView Control using ASP.Net

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

—————————————————————————————–
<body>
<form id=”form1″ runat=”server”>
<div>
<table width=”90%”>
<tr>
<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″ />
<Columns>
<asp:TemplateField HeaderText=”Select”>
<ItemTemplate>
<asp:CheckBox ID=”CheckBox1″ runat=”server” />
<asp:Label ID=”LBLEmpID” runat=”server” Text='<%# container.dataitem(“EmployeeID”) %>’
Visible=”false”></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign=”Left” />
<FooterStyle HorizontalAlign=”Left” />
</asp:TemplateField>
<asp:TemplateField HeaderText=”Name” SortExpression=”LastName”>
<HeaderStyle HorizontalAlign=”Left” />
<ItemTemplate>
<asp:Label ID=”LblName” runat=”server” Text='<%# trim(Eval (“LastName”)) + “, ” + trim( Eval (“FirstName”))%>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=”Title” HeaderText=”Title” SortExpression=”Title”>
<HeaderStyle HorizontalAlign=”Left” />
</asp:BoundField>
<asp:BoundField DataField=”City” HeaderText=”City” SortExpression=”City”>
<HeaderStyle HorizontalAlign=”Left” />
</asp:BoundField>
<asp:TemplateField HeaderText=”HireDate” SortExpression=”HireDate”>
<ItemStyle HorizontalAlign=”Right” />
<HeaderStyle HorizontalAlign=”Right” />
<ItemTemplate>
<asp:Label ID=”LblHireDate” runat=”server” Text='<%# Bind(“HireDate”, “{0:d}”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<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” />
</asp:GridView>
 
</td>
</tr>
<tr>
<td align=”center” colspan=”4″>
<asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” Width=”129px” /></td>
</tr>
<tr>
<td colspan=”4″>
</td>
</tr>
</table>
</div>
</form>
</body>
—————————————————————————————–

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
Try
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)
adpt.Fill(dsData)

DataGV.DataSource = dsData
DataGV.DataBind()
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) colorcheckboxselected.parentElement.parentElement.style.backgroundColor=’#FFAA63′; else { if (RowState==’0′) colorcheckboxselected.parentElement.parentElement.style.backgroundColor=’white'; else colorcheckboxselected.parentElement.parentElement.style.backgroundColor=’#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
str.Append(DataGV.Rows(i).Cells(2).Text)
End If
Next
‘ prints out the result
Response.Write(str.ToString())
End If
End Sub

Regards….

VIJAY MODI

any query Contact: [email protected]