• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 907
  • Last Modified:

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>
0
qwerks
Asked:
qwerks
1 Solution
 
SammyCommented:
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
0
 
sachitjainCommented:
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());
}

Regards
Sachit.
0
 
nguyenvinhtuCommented:
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      }
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now