简体   繁体   English

根据存储过程中的非布尔值检查gridview中的复选框

[英]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. 我试图根据下拉列表中的选择来检查gridview的某些行上的复选框。 I have a gridview where the first column is a checkbox (cb_pain), and the second column is an ID (DrugID). 我有一个gridview,其中第一列是复选框(cb_pain),第二列是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. 存储过程返回需要检查的所有DrugID。 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. 我可以从这样的数据中提取DrugID:dt.Rows(0)(“pain1”)。ToString()这告诉我需要检查的行的DrugID。

Basically I would like to check the checkbox on the row where DrugID = dt.Rows(0)("pain1").ToString() 基本上我想检查DrugID = dt.Rows(0)(“pain1”)行的复选框.ToString()

I think this needs to be done on the selectedindexchange of the dropdown. 我认为这需要在下拉列表的selectedindexchange上完成。

My gridview looks like this: (sorry for the dot's, I couldn't figure out how to tab) 我的gridview看起来像这样:(抱歉点,我无法弄清楚如何标签)

cb_pain........DrugID...............Field1 cb_pain ........ DrugID ...............字段1
x.................3...................other data x ................. 3 ...................其他数据
x.................23.................other data x ................. 23 .................其他数据
x.................24.................other data x ................. 24 .................其他数据
x.................37.................other data x ................. 37 .................其他数据

How can I use this to check the checkbox on the row that has the right DrugID? 如何使用它来检查具有正确DrugID的行上的复选框?

I've tried a couple of different DirectCast things, but no success. 我尝试了几种不同的DirectCast,但没有成功。 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. 我倾向于选择的另一个选项是使用GridView的RowDataBound事件。 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. 你要做的是找到DrugID控件并检查它的值。 If it is what you want, do something. 如果这是你想要的,做点什么。

First, let your GridView know of the event. 首先,让您的GridView知道该事件。

<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. 如果DrugID是23,找到CheckBox并检查它。

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. 这可以在DropDownList的SelectedIndexChanged事件中完成。 Get the list of DrugIDs that need to be checked and iterate each row of your GridView, checking those that match. 获取需要检查的DrugIDs列表并迭代GridView的每一行,检查匹配的那些。

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 : 在.aspx文件中,在复选框中放置一个显式属性:

<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. 然后将检查checkBox,如果gridview绑定上的值为true。 You won't need the ID if your binding is done correctly. 如果绑定正确完成,则不需要ID。

I took jf's response and got it to work when place in the right spot. 我接受了jf的回复,当它放在正确的位置时让它工作。 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. 这遍历我的行并检查正确的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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