Gridview and a Checkbox question

Last post 06-25-2007 8:03 AM by Rivelyn. 7 replies.

Sort Posts:

  • Gridview and a Checkbox question

    06-17-2007, 3:28 PM
    • Participant
      1,020 point Participant
    • Rivelyn
    • Member since 06-20-2006, 5:57 PM
    • Posts 532

    Hey all,

    I have a Gridview that is presenting a list of students that attended a course, I have created a template field with a checkbox that if checked by the instructor it means the student has attended the course.

    Then I have Submit button at the bottom of the page, what I want is for the instructor to just run down the list check each student that did attend the course, then click the Submit button.

    Where I am drawing the blank is how to get each one of those DataKey's which happens to be StudentID into a list if they have been checked so I can then run through my Update statement to make the change in the database.

    Hints, tips, pointers or links to some online info or even a suggestion on a better way to do this would all be greatly appreciated.

    Thanks,

    www.someguy.ca rantings from some Canadian guy
    Follow me on twitter as well twitter.com/SomeCanadianGuy
  • Re: Gridview and a Checkbox question

    06-17-2007, 5:57 PM
    Answer
    • Star
      8,329 point Star
    • farazsk11
    • Member since 02-22-2006, 8:10 PM
    • Sharjah, UAE
    • Posts 1,382

    Hi,

    The button on which you want to check the GirdView's check boxes try to write this code

     

            ArrayList arrList = new ArrayList();
            foreach (GridViewRow dr in GridView1.Rows)
            {
                if (dr.RowType == DataControlRowType.DataRow)
                {
                    if (((CheckBox)dr.FindControl("chkStudent")).Checked)
                    {
                        arrList.Add(GridView1.DataKeys[dr.RowIndex].ToString());
                    }
                }
            }

     For that you have to put a check box in template column and name it like chkStudent. Assign DataKeyNames to the GridView. It will work then.

    Hope it will help you out.

    Thanks and best regards,

    Faraz Shah Khan
    MCP, MCAD.Net, MCSD.Net, MCTS-Win/Web, MCPD-Web
    Blog
  • Re: Gridview and a Checkbox question

    06-17-2007, 7:00 PM
    • Participant
      1,020 point Participant
    • Rivelyn
    • Member since 06-20-2006, 5:57 PM
    • Posts 532

    Thanks farazsk11,

    I will give it a go and post my results.

    www.someguy.ca rantings from some Canadian guy
    Follow me on twitter as well twitter.com/SomeCanadianGuy
  • Re: Gridview and a Checkbox question

    06-17-2007, 7:07 PM
    • Participant
      1,020 point Participant
    • Rivelyn
    • Member since 06-20-2006, 5:57 PM
    • Posts 532

    Any one care to convert to VB? 

    I usually can fake my way through that, but this one I am having problems getting the C# to VB convertion.

    Thanks

    www.someguy.ca rantings from some Canadian guy
    Follow me on twitter as well twitter.com/SomeCanadianGuy
  • Re: Gridview and a Checkbox question

    06-17-2007, 7:32 PM
    • Star
      7,685 point Star
    • agolden
    • Member since 08-03-2002, 10:56 AM
    • Houston, TX
    • Posts 1,036

    Here's an online conversion utility http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx.

    Hope that helps.

    Aaron

    Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
  • Re: Gridview and a Checkbox question

    06-17-2007, 8:26 PM
    • Participant
      1,020 point Participant
    • Rivelyn
    • Member since 06-20-2006, 5:57 PM
    • Posts 532

    Here is the final code for anyone ever trying to the same thing, it works like a charm thank you to the two people that helped me out with this.

    Dim sConnStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.MDF;Integrated Security=True;User Instance=True"

    Dim cnBKPost As New SqlConnection(sConnStr)

    Dim con As New SqlConnection(sConnStr)

    Dim arrList As New ArrayList()

    For Each dr As GridViewRow In GridView1.Rows

    If dr.RowType = DataControlRowType.DataRow Then

    If DirectCast(dr.FindControl("chkStudent"), CheckBox).Checked Then

    arrList.Add(GridView1.DataKeys(dr.RowIndex).Value.ToString)

    End If

    End If

    Next

    Dim i As Integer

    For i = 0 To arrList.Count - 1

    Const SQL As String = "UPDATE Trainee SET Attended = @Attended WHERE (TraineeID = @TraineeID)"

    Dim myCommand As New SqlCommand(SQL, cnBKPost)

    myCommand.Parameters.AddWithValue("@Attended", "Attended")

    myCommand.Parameters.AddWithValue("@TraineeID", arrList.Item(i))

    cnBKPost.Open()

    myCommand.ExecuteNonQuery()

    cnBKPost.Close()

    Next

    End Sub

    www.someguy.ca rantings from some Canadian guy
    Follow me on twitter as well twitter.com/SomeCanadianGuy
  • Re: Gridview and a Checkbox question

    06-20-2007, 12:23 PM
    • Member
      24 point Member
    • storm721984
    • Member since 06-19-2007, 6:06 PM
    • Posts 51

    I'm a little confuse but this code actually allow changing value to the GridView from an external control right?

  • Re: Gridview and a Checkbox question

    06-25-2007, 8:03 AM
    • Participant
      1,020 point Participant
    • Rivelyn
    • Member since 06-20-2006, 5:57 PM
    • Posts 532

    No this allows you to change a value in a gridview, with a check box control within the gridview. In this instance the code allowed me to check many rows in a single gridview then click a process button that change a value of attended from false to true within the database.

    www.someguy.ca rantings from some Canadian guy
    Follow me on twitter as well twitter.com/SomeCanadianGuy
Page 1 of 1 (8 items)