Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

datagrid checkbox asp.net c#

datagrid checkbox asp.net c#

datagrid checkbox asp.net c#

I am using a datagrid where the first column consists of check boxes and the second column is bound to a database table.

         <asp:CheckBox id="chkSelect" runat="server" ></asp:CheckBox>

    <asp:BoundColumn DataField="name" HeaderText="Name"></asp:BoundColumn>

When I press the submit button, I want to save the selected names (selected via the checkbox) in some sort of a format (like ArrayList or Vector)

Has anyone got any examples of such?

Also, so far I just wanted to check if the selection works and if I can actually count the number of selected rows.  

So I used the the following code:

private void Submit_Button_Click(object sender, System.EventArgs e)
int rowCount = 0;

   foreach(DataGridItem resourcesDataGridItem in resourcesDataGrid.Items)
         CheckBox myCheckbox = (CheckBox)resourcesDataGridItem.Cells[0].Controls[1];
        if(myCheckbox.Checked == true)
    resultlbl.Text = rowCount.ToString();

Unfortunately it does not seem to work as it resets to 0 each time. If I mark the checkbox in the datagrid as Checked then each time it will return the number of check boxes (because they are all selected)

What am I doing wrong?


RE: datagrid checkbox asp.net c#

Here's what I used that get the index number of the rows that is checked, creates arraylist, and passed into a querystring

Sub OnNext (sender as object, e as EventArgs)
  Dim selectedRows as ArrayList
  selectedRows = new ArrayList()
  Dim aitem as DataGridItem
  for each aitem In DataGrid1.Items
      Dim chkBox as CheckBox
    chkBox = aitem.FindControl("selectRow")
    if (chkBox.Checked)
        dim id as Textbox
        id = aitem.FindControl("ID")
    end if
  next aitem    
  if (selectedRows.Count > 0)
      dim nextURL as Stringbuilder
    nextURL = new StringBuilder ("printquote.aspx?")
    Dim productID as string
    for each productID in selectedRows
    next productID    
  End if
End Sub

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close