简体   繁体   中英

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

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

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.

I took jf'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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM