0

现在我有一个数据库并提取该数据并将其显示为表格,我有一个组框和单选按钮序列,在每个组框(groupbox1,groupbox2 等......)中有 2 个单选按钮,即 rdbtn1Yes 和 rdbtn1No(然后它在下一个 Groupbox 中增加 +1)。现在我for loop用来浏览每个组框和单选按钮。这是我的代码:

    Dim sqlda As New SqlDataAdapter("SELECT * FROM table1 WHERE column1= '" & lblWONo.Text & "'", Constr)
    Dim sqlds As New DataSet

    sqlds.Clear()
    sqlda.Fill(sqlds)

    If sqlds.Tables(0).Rows.Count > 0 Then

        With sqlds.Tables(0).DefaultView.Item(0)

            txtDateCreated.Value = .Item(0).ToString
            txtComments.Text = .Item(1).ToString

            'check column if it contain FALSE/TRUE value
            'then toggle the radiobutton state to TRUE

            'In this part i know there is another/easiest way to checked radio buttons to TRUE value 
            'and this is my code using looping (below):

            If .Item(2) = False Then
                rdbtn1No.Checked = True
            Else
                rdbtn1Yes.Checked = True
            End If

            If .Item(3) = False Then
                rdbtn2No.Checked = True
            Else
                rdbtn2Yes.Checked = True
            End If

            If .Item(4) = False Then
                opt3N.Checked = True
            Else
                opt3Y.Checked = True
            End If
        End With
    End If

循环示例代码:

            Dim itemNo As Integer
            Dim rdbtnSet As Integer = 1
            Dim grpboxCnt As Integer = 1

             For Each grpbx As GroupBox In Me.Controls.OfType(Of GroupBox)()
                For itemNo = 2 To sqlds.Tables(0).Columns.Count

                    If .Item(itemNo) = True Then
                        rdbtn & rdbtnSet & "Yes".checked = True 'I want to be this way but we know that this is not working or its not the proper way. That is my problem.
                    Else 
                        rdbtn & rdbtnSet & "No".checked = True 'I want to be this way but we know that this is not working or its not the proper way. That is my problem.
                    End If

                Next
                 rdbtnSet += 1
                 grpboxCnt += 1
             Next

就这样。先感谢您!

4

1 回答 1

1

考虑使用字典(id、控件)来存储您的控件。然后迭代字典并设置你的状态。

于 2013-08-15T08:39:38.967 回答