Accessing a checkbox inside a DataGrid

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" />
Who is Participating?
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());

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");

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      }
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.