gogetsome
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(ByVa l sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs ) Handles gvSkills.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim objConn As SqlConnection = New SqlConnection(System.Confi guration.C onfigurati onManager. Connection Strings("E ngineering Skills").T oString)
Try
Dim objCmd As SqlCommand = New SqlCommand("Get_RequestSki lls", objConn)
objCmd.CommandType = CommandType.StoredProcedur e
Dim Request_Id As SqlParameter = objCmd.Parameters.Add("@Re quest_Id", SqlDbType.Int)
Request_Id.Value = Session("Request_Id").ToSt ring
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).It em("Skill_ Id")
Dim Skill As Label = CType(e.Row.FindControl("l blSkill_Id "), Label)
Dim s As Integer = Skill.Text
Dim CheckIt As CheckBox = CType(e.Row.FindControl("c kbSkill"), 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
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(ByVa
If e.Row.RowType = DataControlRowType.DataRow
Dim objConn As SqlConnection = New SqlConnection(System.Confi
Try
Dim objCmd As SqlCommand = New SqlCommand("Get_RequestSki
objCmd.CommandType = CommandType.StoredProcedur
Dim Request_Id As SqlParameter = objCmd.Parameters.Add("@Re
Request_Id.Value = Session("Request_Id").ToSt
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
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).It
Dim Skill As Label = CType(e.Row.FindControl("l
Dim s As Integer = Skill.Text
Dim CheckIt As CheckBox = CType(e.Row.FindControl("c
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
ASKER
Thank you for assisting!
I changed the line to this:
Dim CheckedSkill As Integer = objDS.Tables(0).Rows(i).It em("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("l blSkill_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.
I changed the line to this:
Dim CheckedSkill As Integer = objDS.Tables(0).Rows(i).It
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("l
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
Also you code will eventually throw an exception because this:
For i As Integer = (0) To iRows <=== Need to be iRows - 1
ASKER
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_SelectedInde xChanged(B yVal sender As Object, ByVal e As System.EventArgs) Handles gvRequestList.SelectedInde xChanged
Dim row As GridViewRow = gvRequestList.SelectedRow
Session("Request_Id") = (CType(row.Cells(1).FindCo ntrol("lbl Request_Id "), Label)).Text
pnlRequest.Visible = True
pnlRequestList.Visible = False
GetRequest()
gvSkills.DataBind()
End Sub
Is the problem due to binding order??
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_SelectedInde
Dim row As GridViewRow = gvRequestList.SelectedRow
Session("Request_Id") = (CType(row.Cells(1).FindCo
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
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
ASKER
I commented out the Session("Request_Id") = ""
The dataset value does loop.
Dim CheckedSkill As Integer = objDS.Tables(0).Rows(i).It em("Skill_ Id")
but the
Dim Skill As Label = CType(e.Row.FindControl("l blSkill_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
The dataset value does loop.
Dim CheckedSkill As Integer = objDS.Tables(0).Rows(i).It
but the
Dim Skill As Label = CType(e.Row.FindControl("l
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?
Where did you assign value to the session?
>>>It's not Session("Request_Id") = "" causing the problem.
I meant "Not only". Sorry.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you!
Dim CheckedSkill As Integer = objDS.Tables(0).Rows(0).It
You are always checking the row(0), the first row.