Link to home
Start Free TrialLog in
Avatar of qwerks
qwerks

asked on

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 {
            conPubs.Open();
            IDataReader reader = cmdSelect.ExecuteReader ();
            dgHistory.DataSource = reader;
            dgHistory.DataBind();
      }
            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());
            }

      */
   }      


</script>


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

      <Columns>
      <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">
            <ItemTemplate>
                  <%#DataBinder.Eval(Container.DataItem,"Amount","{0:C}")%>
            </ItemTemplate>
      </asp:TemplateColumn>
      
      <asp:TemplateColumn Headertext = "Cancel">
            <ItemTemplate>
                  <asp:CheckBox id = "cancel" runat="server"/>
            </ItemTemplate>
      </asp:TemplateColumn>
      </Columns>
</asp:datagrid>
<asp:Button id = "btnCancel" OnClick = "Click_Cancel" text = "Cancel " runat = "server" />
</form>
</body>
</html>
Avatar of Sammy
Sammy
Flag of Canada image

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");
Response.Write(myCheckBox.isChecked.ToString());

HTH
ASKER CERTIFIED SOLUTION
Avatar of sachitjain
sachitjain
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nguyenvinhtu
nguyenvinhtu

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
So,...
//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++)
{
      if(((CheckBox)dgTransfer.Items[i].FindControl("cancel")).Checked==true)
      {
            //Do something here      }
}