asp.netvb.netgridviewcheckboxdirectcast

Check Checkboxes in a gridview based on a non-boolean value from a stored procedure


I am trying to check checkboxes on certain rows of a gridview based off of a selection from a dropdown. I have a gridview where the first column is a checkbox (cb_pain), and the second column is an ID (DrugID). The user makes a selection from the dropdown list, this fires a stored procedure. The stored procedure returns all of the DrugID's that need to be checked. I'm able to pull the DrugID from the data like this: dt.Rows(0)("pain1").ToString() That tells me DrugID for a row that needs to be checked.

Basically I would like to check the checkbox on the row where DrugID = dt.Rows(0)("pain1").ToString()

I think this needs to be done on the selectedindexchange of the dropdown.

My gridview looks like this: (sorry for the dot's, I couldn't figure out how to tab)

cb_pain........DrugID...............Field1
x.................3...................other data
x.................23.................other data
x.................24.................other data
x.................37.................other data

How can I use this to check the checkbox on the row that has the right DrugID?

I've tried a couple of different DirectCast things, but no success. Can anyone point me in the right direction?

Thanks


Solution

  • Another option that I tend to prefer is to make use of the RowDataBound event of the GridView. As each row gets data bound to it, this event gets called. What you would do is find the DrugID control and check its value. If it is what you want, do something.

    First, let your GridView know of the event.

    <asp:GridView ID="gvDrugs" runat="server" OnRowDataBound="gvDrugs_RowDataBound">
    </asp:GridView>
    

    Then handle the event. If the DrugID is 23, find the CheckBox and check it.

    Protected Sub gvDrugs_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then    
            Dim lblDrugID As Label = e.Row.FindControl("lblDrugID")
            If lblDrugID.Text = "23" Then
                Dim cbPain As CheckBox = e.Row.FindControl("cbPain")
                cbPain.Checked = True
            End If
        End If
    End Sub
    

    **Note here that I am assuming the types of controls to be labels and checkboxes. You have not shown your markup, so this is the best I can do.

    After your edit, I would probably agree with you. This is something that could be done in the SelectedIndexChanged event of the DropDownList. Get the list of DrugIDs that need to be checked and iterate each row of your GridView, checking those that match.

    Protected Sub dropdown_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    
        Dim uniqueDrugIDs As HashSet(Of String) = New HashSet(Of String)()
    
        ' Here we assume dt is some sort of global variable as your question above implies
        For Each dr As DataRow In dt.Rows
            uniqueDrugIDs.Add(dr("drugID").ToString())
        Next
    
        For Each gvRow As GridViewRow In gvDrugs.Rows
            Dim lblDrugID As Label = gvRow.FindControl("lblDrugID")
            If (uniqueDrugIDs.contains(lblDrugID.Text)) Then
                Dim cbPain As CheckBox = gvRow.FindControl("cbPain")
                cbPain.Checked = True
            End If
        Next
    
    End Sub