我正在尝试根据数据集的结果检查我的复选框。 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>
答案 0 :(得分:1)
填充网格时,请删除您发布的for
个字词。而是在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