Skip to content

How to use checkbox in datagrid using asp.net with csharp code

narasiman rao

Asked Sep 7, 2012, 8:29 PM EDT2 answers
Share
LinkedInX
how to use checkbox in datagrid using asp.net with csharp code
please send the design  and coding 

Regards,
Rao.

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Akkiraju Ivaturi
    0
    Otherwise, use this code:
    <%@ Page Language="vb" Src="ChkBoxDataGrid.aspx.vb" Inherits="DotNetJohn.ChkBoxDataGrid" %>
     <!DOCTYPE html PUBLIC "-//W3C//DTD html 4.0 Transitional//EN">
     
     
     
     
     
     
     
     
     
     
     
     

     
                   AutoGenerateColumns="False"
                   BorderColor="#999999" 
                   BorderStyle="None" 
                   BorderWidth="1px" 
                   BackColor="White" 
                   CellPadding="3" 
                   GridLines="Vertical">
       
         
           
             
                        Text='<%# DataBinder.Eval(Container.DataItem,"CompanyName") %>'
                        runat="server" />
           
         
         
         
         
         
         
         
         
           
             
                        Text='<%# DataBinder.Eval(Container.DataItem,"CustomerID") %>'
                        Runat="server" />
           
         
         
           
             
           
         
       
       
       
       
     
     
                Runat="server"
                Font-Name="Verdana"
                Font-Size="10px" />
     
     
      

    Imports System.Data
     Imports System.Data.SqlClient
     Imports System.Configuration
     Imports System.Web.UI.WebControls
     
     Namespace DotNetJohn
     
       Public Class ChkBoxDataGrid : Inherits System.Web.UI.Page
         Protected btnShow As Button
         Protected dtgCusts As DataGrid
         Protected lblSelections As Label
     
         Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           If Not IsPostBack Then
             btnShow.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to delete these rows?')")
             BindTheData
           End If
         End Sub
     
         Sub BindTheData
           Dim objConn As SqlConnection
           Dim objCmd As SqlCommand
           objConn = New SqlConnection(ConfigurationSettings.AppSettings("NorthwindConnection"))
           Dim strSql As String
           strSql = "SELECT Top 10 CustomerID, CompanyName, ContactName, " _
                  & "ContactTitle, City, Country, Phone FROM Customers"
           Try
             objCmd = New SqlCommand(strSql, objConn)
             objConn.Open
             dtgCusts.DataSource = objCmd.ExecuteReader()
             dtgCusts.DataBind()
           Catch
           Finally
             If objConn.State = ConnectionState.Open Then
               objConn.Close()
               objConn.Dispose()
             End If
           End Try
         End Sub
     

     Sub ShowSelections(sender As System.Object, e As System.EventArgs)
           Dim dgItem As DataGridItem
           Dim chkSelected As CheckBox
           Dim strCompanyName As String
           Dim strCustomerID As String
     
           lblSelections.Text = "
    Fooled Ya! The following rows were marked for deletion, " 
           lblSelections.Text += "but not actually deleted:

    "
           For Each dgItem in dtgCusts.Items
             chkSelected = dgItem.FindControl("chkSelection")
             If chkSelected.Checked Then
               strCompanyName = CType(dgItem.FindControl("lblCompanyName"), Label).Text
               strCustomerID = CType(dgItem.FindControl("lblCustomerId"), Label).Text
               lblSelections.Text += "Company Name: " & strCompanyName & " | " 
               lblSelections.Text += "Customer ID: " & strCustomerID & "
    " 
             End If
           Next
         End Sub
     
       End Class
     
     End Namespace 
    0