Link to home
Start Free TrialLog in
Avatar of gogetsome
gogetsomeFlag for United States of America

asked on

Loop through dataset check checkbox in gridview rowdatabound

Hello, I have a gridview that is populated from a stored procedure. In one itemtemplate I have lblSkill_Id and in another itemtemplate I have a ckbSkill. That runs great and I have a gridview populated with several rows of the skill_id and an empty checkbox.
Now, on the rowdatabound for the gridview fill a dataset of skill_Id's for the given record. I need to loop through the datset and populate the checkboxes in the gridview where the value of the gridviews lblSkill_Id = the value of each skill_id of the dataset.


With my code below I have an inifinite loop issue where the first row in the dataset keep on being used without going to the next row. How can i fix this????

 Protected Sub gvSkills_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSkills.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim objConn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("EngineeringSkills").ToString)
            Try
                Dim objCmd As SqlCommand = New SqlCommand("Get_RequestSkills", objConn)
                objCmd.CommandType = CommandType.StoredProcedure
                Dim Request_Id As SqlParameter = objCmd.Parameters.Add("@Request_Id", SqlDbType.Int)
                Request_Id.Value = Session("Request_Id").ToString
                Session("Request_Id") = ""

                objConn.Open()

                Dim objDA As New SqlDataAdapter(objCmd)
                Dim objDS As New DataSet("RequestSkills")

                objDA.Fill(objDS)

                If objDS.Tables(0).Rows.Count = 0 Then
                Else
                    Dim iRows As Integer = objDS.Tables(0).Rows.Count
                    For i As Integer = (0) To iRows
                        Dim CheckedSkill As Integer = objDS.Tables(0).Rows(0).Item("Skill_Id")


                        Dim Skill As Label = CType(e.Row.FindControl("lblSkill_Id"), Label)
                        Dim s As Integer = Skill.Text
                        Dim CheckIt As CheckBox = CType(e.Row.FindControl("ckbSkill"), CheckBox)
                        If CheckedSkill = s Then
                            CheckIt.Checked = True
                        End If

                    Next i

                End If
            Catch ex As Exception
                lblMessage.Text = ex.ToString
            Finally
                objConn.Close()

            End Try
        End If
    End Sub
Avatar of prairiedog
prairiedog
Flag of United States of America image

Here:
Dim CheckedSkill As Integer = objDS.Tables(0).Rows(0).Item("Skill_Id")

You are always checking the row(0), the first row.
Avatar of gogetsome

ASKER

Thank you for assisting!
I changed the line to this:
   Dim CheckedSkill As Integer = objDS.Tables(0).Rows(i).Item("Skill_Id")

It now loops through the dataset, but now when coming through the next row of the dataset the value of
 Dim Skill As Label = CType(e.Row.FindControl("lblSkill_Id"), Label)
                        Dim s As Integer = Skill.Text
does not loop to the next row of the gridview

so even though I'm looping through the dataset the condition is only evaluated for the first row of the gridview.
Where do you bind your GridView? Can I take a look at your Page_Load?
Also you code will eventually throw an exception because this:
For i As Integer = (0) To iRows  <=== Need to be iRows - 1
Thank you! I fixed the iRows - 1

The gvSkills is populated on the SelectedindexChanged of another gridview. The sproc just gets all the skills form the skills table and thows them in the gvSkills. In GetRequest() of the same event I populate a session with the primary key of RequestSkills table where the dataset gets the skill(s) for the requested record. Which is why I need to loop through the dataset of the records skills and check the checkbox of the gvskills. The idea is that the user can add or remove skills. When the user is finished I will update the records skills.

Here is where I populate the gvSkills:

Protected Sub gvRequestList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvRequestList.SelectedIndexChanged
        Dim row As GridViewRow = gvRequestList.SelectedRow
        Session("Request_Id") = (CType(row.Cells(1).FindControl("lblRequest_Id"), Label)).Text
        pnlRequest.Visible = True
        pnlRequestList.Visible = False
        GetRequest()
        gvSkills.DataBind()
    End Sub



Is the problem due to binding order??
Because you are using Session("Request_Id") to store your data. After the first row, you did this:
Session("Request_Id") = ""
so there is no data in the session, and when it comes to the second row, it will find empty string in Session("Request_id"), so your Request_id.Value is nothing. No data will be loaded then. Can you confirm that after the first datarow, this line is always true?
     If objDS.Tables(0).Rows.Count = 0 Then
I commented out the Session("Request_Id") = ""

The dataset value does loop.
 Dim CheckedSkill As Integer = objDS.Tables(0).Rows(i).Item("Skill_Id")

but the

 Dim Skill As Label = CType(e.Row.FindControl("lblSkill_Id"), Label)
                        Dim s As Integer = Skill.Text


Where I want to get the next row of the gridview to evaluate does not change.

So this condition only truely validates the first row of the gridview against the first row of the dataset.
 If CheckedSkill = s Then
                            CheckIt.Checked = True
                        End If

It's not Session("Request_Id") = "" causing the problem.
Where did you assign value to the session?
>>>It's not Session("Request_Id") = "" causing the problem.
I meant "Not only". Sorry.
As far as I can see, the purpose of looping through your dataset is to find one row that matches the Skill_id. Once found, check the checkbox, right?
ASKER CERTIFIED SOLUTION
Avatar of prairiedog
prairiedog
Flag of United States of America 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
Thank you!