3

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

glitzsfa
  • 353
  • 1
  • 2
  • 14

3 Answers3

2

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
j.f.
  • 3,908
  • 2
  • 29
  • 42
  • I like what you've done here. You were correct on your assumption about labels and check boxes. My grid is already loaded and databound by the time the user selects something from the dropdown. I need to probably fire this off after a selectedindexchange on the dropdown. How can I modify your code to check the correct boxes after the dropdown has been changed? – glitzsfa Nov 20 '15 at 20:35
  • It's kind of hard to say without some more information. If you can add what you actually need to happen in the question, that would help. For example, what does the dropdown actually do? Does it change the data source? Does it change the condition for checked checkboxes? etc... – j.f. Nov 20 '15 at 20:41
  • Take a look at my edit. That should at least point you in the right direction. – j.f. Nov 20 '15 at 21:36
1

You can go for that kind of loop that will go through all your rows :

for (int x = 0; x < NameOfYourGrid.Rows.Count; x++)
{

  GridViewRow row = (GridViewRow)NameOfYourGrid.Rows[x];

     if(row("pain1").ToString() == "23")
      CheckBox chk  = (CheckBox)row.Cells[0].FindControl("The_Name_Of_The_Control");

}

Or, if you wich to do it on the binding ;

In the .aspx file put an explicit property on your check box :

<asp:CheckBox ID="chkInside" runat="server" Checked='<%# CheckForChecked(Container.DataItem as Whatever_the_object_is) %>' ></asp:CheckBox>

And link that event in the code behind :

    protected bool CheckForChecked(Whatever_the_object_is OBJ)
    {
        try
        {
            return (bool) OBJ.Boolean_value;
            // here you are supposed to have a compete Object
            //  OBJ.ID is suposed to be the rigth id
        }
        catch
        {
            return false;
        }
    }

Then the checkBox will be checked if the value is true on the gridview binding. You won't need the ID if your binding is done correctly.

Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62
0

I took j-f's response and got it to work when place in the right spot. At the end of filling the dataset I added this code:

    Dim row As GridViewRow
    For Each row In gv_pain.Rows

        If row.RowType = DataControlRowType.DataRow Then
            Dim lblDrugID As Label = row.FindControl("lbl_DrugID")
            If lblDrugID.Text = dt.Rows(0)("pain1").ToString() Then
                Dim cbPain As CheckBox = row.FindControl("CheckBoxPain")
                cbPain.Checked = True
            End If
        End If
    Next

This goes through my rows and check the correct ones.

glitzsfa
  • 353
  • 1
  • 2
  • 14
  • Just so you know, this only checks "pain1" of the first row of your datatable. If you have more than one "pain1" that must be checked or something, this will not work correctly. – j.f. Nov 20 '15 at 21:45