1

我正在尝试根据数据集的结果检查我的复选框。Memory_Error 是数据库中的列。保存数据时,我将插入 1 或 0 作为字符串。我已经查看并查看并寻找有关如何执行此操作的示例,但似乎没有一个可以完成工作。我没有收到任何错误,我只是在应该检查时得到一个未选中的复选框。提前感谢您的任何帮助。如果我的解释不够简洁,我深表歉意。

这是我的 vb.net 代码。

 Dim dap2 As New SqlDataAdapter("Select ID, Store, Status, Reason, Detail, Detail_1, Detail_2,  Detail_3, Information, Memory_Error, Detail_4 FROM PollDetail where Poll_Date = '" & pollDate & "'", conn)
    conn.Open()
    Dim ds2 As New DataSet
    dap2.Fill(ds2, "PollDet")

    gvEmailRes2.DataSource = ds2

    Dim i As Integer

    For i = 0 To gvEmailRes2.Rows.Count - 1

        '-------------ckbMemErr-----------------------------------------------------------------/////
        If ds2.Tables("PollDet").Rows(i).Item(9) = "1" Then
            Dim row2Fill As GridViewRow = gvEmailRes2.Rows(i)
            Dim cbMemErr As CheckBox = DirectCast(row2Fill.FindControl("ckbMemErr"), CheckBox)
            cbMemErr.Checked = True

        ElseIf ds2.Tables("PollDet").Rows(i).Item(9) = "0" Then
            Dim row2Fill As GridViewRow = gvEmailRes2.Rows(i)
            Dim cbMemErr As CheckBox = DirectCast(row2Fill.FindControl("ckbMemErr"), CheckBox)
            cbMemErr.Checked = False

        End If
 Next

    gvEmailRes2.DataBind()
    conn.Close()
    dap2.Dispose()

这是 ASP.NET

<asp:TemplateField HeaderText="Memory Error" SortExpression="Memory_Error" ItemStyle-    VerticalAlign="Top">
 <ItemTemplate>
  <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
    <br />
  <asp:CheckBox ID="ckbMemErr" runat="server" Text="Memory Error" Font-Size="11px" />
 </ItemTemplate>
<ItemStyle VerticalAlign="Top" />
</asp:TemplateField>
4

1 回答 1

1

填充网格时,请删除for您发布的 cicle。RowDataBound而是在事件中放入相同的逻辑:

第 1 步 - 加载数据

Dim dap2 As New SqlDataAdapter("Select ID, Store, Status, Reason, Detail, Detail_1, Detail_2,  Detail_3, Information, Memory_Error, Detail_4 FROM PollDetail where Poll_Date = '" & pollDate & "'", conn)
conn.Open()
Dim ds2 As New DataSet
dap2.Fill(ds2, "PollDet")
gvEmailRes2.DataSource = ds2
gvEmailRes2.DataBind()
conn.Close()
dap2.Dispose()

第 2 步 - 设置复选框

Private Sub gvEmailRes2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvEmailRes2.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim chk As Checkbox = CType(e.Row.FindControl("ckbMemErr"), Checkbox)
        Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView)

        If Not chk Is Nothing and Not drv Is Nothing Then
            Dim err As Integer = 0
            If drv.Item("Memory_Error") <> "" and not IsDbNull(drv.Item("Memory_Error"))  Then
                err = CInt(drv.Item("Memory_Error"))
            End If
            If err = 1 Then
                chk.checked = True
            Else
                chk.checked = False
            End If
        End If
    End If
End Sub
于 2013-05-22T22:04:59.227 回答