Accessing a checkbox inside a DataGrid

Posted on 2006-05-31
Last Modified: 2011-10-03
Hello.  I am new to ASP.NET and need help on how to access checkbox inside a datagrid that is populated by a SQL database.  I have the pseudocode written in the void Click_Cancel (Object s, EventArgs E) function.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat = "server">

void Page_Load(Object Source, EventArgs E)

      //populate from account
      if (!IsPostBack){

            SqlConnection conPubs;
            SqlCommand cmdSelect;
            SqlDataReader dtrResults;

            conPubs = new SqlConnection("connect line" );
            cmdSelect = new SqlCommand( "select statement", conPubs);

      try {
            IDataReader reader = cmdSelect.ExecuteReader ();
            dgHistory.DataSource = reader;
            finally { conPubs.Dispose(); }


void Click_Cancel (Object s, EventArgs E) {


            Code here to access to checked the column cancel and see if it is checked, if it is checked then display the amount

            for (i = 0; i < dgTransfer.Column.Count(), ++i) {
                  if (dgTransfer.Column [i].Cancel.isChecked == true)
                        Response.Write (dgTransfer.Column [i].Amount.ToString());



<form runat = "server">
<asp:datagrid id = dgHistory runat = "server" AutoGenerateColumns = "false">

      <asp:EditCommandColumn UpdateText="Update"/>
      <asp:BoundColumn HeaderText = "Transaction Number" DataField = "transaction_number" ReadOnly= "true"/>
      <asp:BoundColumn HeaderText = "Date Transfer" DataField = "date_transfer" ReadOnly= "true"/>
      <asp:TemplateColumn HeaderText="Amount" ItemStyle-HorizontalAlign="Right">
      <asp:TemplateColumn Headertext = "Cancel">
                  <asp:CheckBox id = "cancel" runat="server"/>
<asp:Button id = "btnCancel" OnClick = "Click_Cancel" text = "Cancel " runat = "server" />
Question by:qwerks
    LVL 27

    Expert Comment

    why dont you use findcontrol after you start your loop and check for if the checkbox is checked or not
    CheckBox myCheckBox = (CheckBox) (dgTransfer.FindControl("cancel");

    LVL 12

    Accepted Solution

    Actually u need to do like

    for (i = 0; i < dgTransfer.Items.Count, i++) {
          CheckBox chk = (CheckBox)dgTransfer.Items[i].Cells[1].FindControl("cancel")
          if (chk.isChecked == true)
                Response.Write (dgTransfer.Items[i].Cells[0].Text.ToString());

    LVL 4

    Expert Comment

    Hi qweks,
    //This is your code:
    for (i = 0; i < dgTransfer.Column.Count(), ++i) {
                   if (dgTransfer.Column [i].Cancel.isChecked == true)
                        Response.Write (dgTransfer.Column [i].Amount.ToString());
    1- Column is to move vertical, so there is no column which contains checkbox and "Amount"  data. OK, but that's row. And in DataGrid, we has item which is same as row
    //You should use to this instead
    Before this code, you should check to see if there is any data in Grid or it may throw Exception.
    for(int i=0;i<dgTransfer.Items.Count;i++)
                //Do something here      }

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now