0

我目前正在使用 VB.NET 和 SQL Server 开发数据库系统,除了我的 CheckListBox 和其他对象之外,一切正常。

我创建了一个事件,每当我单击 DataGridView 中的一个单元格时,其中的位(数据类型)数据将显示在我的 CheckListBox 上,并根据我单击的单元格更改值。

问题是:我单击一次并根据我单击的单元格检查复选框,但是一旦我单击另一个具有不同位值的单元格,复选框不会更改,然后其余代码不会更改不工作。

这是我使用的代码示例:

 Private Sub dgvMain_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dgvMain.CellClick
 Try

     /*These codes work....*/

            dtpReg.Text = dgvMain.Item(1, e.RowIndex).Value
            txtFirstName.Text = dgvMain.Item(2, e.RowIndex).Value
            txtMiddleName.Text = dgvMain.Item(3, e.RowIndex).Value
            txtLastName.Text = dgvMain.Item(4, e.RowIndex).Value
            If dgvMain.Item(5, e.RowIndex).Value = True Then
                optMale.Checked = True
            Else
                optFemale.Checked = True
            End If
            dtpBirthdate.Text = dgvMain.Item(6, e.RowIndex).Value
            cboCivilStatus.Text = dgvMain.Item(8, e.RowIndex).Value
            dtpResidency.Text = dgvMain.Item(9, e.RowIndex).Value
            txtSt.Text = dgvMain.Item(10, e.RowIndex).Value
            cboBrgy.Text = dgvMain.Item(11, e.RowIndex).Value
            cboEducation.Text = dgvMain.Item(12, e.RowIndex).Value
            chkStudy.Checked = dgvMain.Item(13, e.RowIndex).Value
            chkTB.Checked = dgvMain.Item(14, e.RowIndex).Value
            chkMalnourished.Checked = dgvMain.Item(15, e.RowIndex).Value
            chkIllDisabled.Checked = dgvMain.Item(16, e.RowIndex).Value
            chkSoloParent.Checked = dgvMain.Item(17, e.RowIndex).Value
            chkActive.Checked = dgvMain.Item(19, e.RowIndex).Value
            If dgvMain.Item(20, e.RowIndex).Value = "Public center/hospital" Then
                optPublic.Checked = True
            ElseIf dgvMain.Item(20, e.RowIndex).Value = "Private center/hospital" Then
                optPrivate.Checked = True
            ElseIf dgvMain.Item(20, e.RowIndex).Value = "Non-professional" Then
                optNonProf.Checked = True
            End If
            txtTelephone.Text = dgvMain.Item(21, e.RowIndex).Value
            txtMobile.Text = dgvMain.Item(22, e.RowIndex).Value
            txtEmail.Text = dgvMain.Item(23, e.RowIndex).Value

  /*Unemployed ----> This is where the CheckListBox problem occurs*/

            If dgvMain.Item(24, e.RowIndex).Value = True Then

                clbUnemployed.SetItemCheckState(0, CheckState.Checked)
            Else
                clbUnemployed.SetItemCheckState(0, CheckState.Unchecked)
            End If
            If dgvMain.Item(25, e.RowIndex).Value = True Then
                clbUnemployed.SetItemCheckState(1, CheckState.Checked)
            Else
                clbUnemployed.SetItemCheckState(1, CheckState.Unchecked)
            End If
            If dgvMain.Item(26, e.RowIndex).Value = True Then
                clbUnemployed.SetItemCheckState(2, CheckState.Checked)
            Else
                clbUnemployed.SetItemCheckState(2, CheckState.Unchecked)
            End If
            If dgvMain.Item(27, e.RowIndex).Value = True Then
                clbUnemployed.SetItemCheckState(3, CheckState.Checked)
            Else
                clbUnemployed.SetItemCheckState(3, CheckState.Unchecked)
            End If
            If dgvMain.Item(28, e.RowIndex).Value = True Then
                clbUnemployed.SetItemCheckState(4, CheckState.Checked)
            Else
                clbUnemployed.SetItemCheckState(4, CheckState.Unchecked)
            End If
            If dgvMain.Item(29, e.RowIndex).Value = True Then
                clbUnemployed.SetItemCheckState(5, CheckState.Checked)
            Else
                clbUnemployed.SetItemCheckState(5, CheckState.Unchecked)
            End If
            If dgvMain.Item(30, e.RowIndex).Value = True Then
                clbUnemployed.SetItemCheckState(6, CheckState.Checked)
            Else
                clbUnemployed.SetItemCheckState(6, CheckState.Unchecked)
            End If
            If dgvMain.Item(31, e.RowIndex).Value = True Then
                clbUnemployed.SetItemCheckState(7, CheckState.Checked)
            Else
                clbUnemployed.SetItemCheckState(7, CheckState.Unchecked)
            End If
            If dgvMain.Item(32, e.RowIndex).Value = True Then
                clbUnemployed.SetItemCheckState(8, CheckState.Checked)
            Else
                clbUnemployed.SetItemCheckState(8, CheckState.Unchecked)
            End If
            If dgvMain.Item(33, e.RowIndex).Value = True Then
                clbUnemployed.SetItemCheckState(9, CheckState.Checked)
            Else
                clbUnemployed.SetItemCheckState(9, CheckState.Unchecked)
            End If
            If dgvMain.Item(34, e.RowIndex).Value = True Then
                clbUnemployed.SetItemCheckState(10, CheckState.Checked)
            Else
                clbUnemployed.SetItemCheckState(10, CheckState.Unchecked)
            End If
            If dgvMain.Item(35, e.RowIndex).Value = True Then
                clbUnemployed.SetItemCheckState(11, CheckState.Checked)
            Else
                clbUnemployed.SetItemCheckState(11, CheckState.Unchecked)
            End If
            If dgvMain.Item(36, e.RowIndex).Value = True Then
                clbUnemployed.SetItemCheckState(12, CheckState.Checked)
            Else
                clbUnemployed.SetItemCheckState(12, CheckState.Unchecked)
            End If

/*Employment ---> the code after this doesn't work. Text doesn't display. Not one RadioButton checked. No picture on the picturebox. Why?*/

            cboIndustry.Text = dgvMain.Item(37, e.RowIndex).Value
            txtSalary.Text = dgvMain.Item(38, e.RowIndex).Value
            cboJobStatus.Text = dgvMain.Item(39, e.RowIndex).Value
            If dgvMain.Item(40, e.RowIndex).Value = True Then
                optGyes.Checked = True
            Else
                optGno.Checked = True
            End If
            txtBrgySubd.Text = dgvMain.Item(41, e.RowIndex).Value
            txtCityMunicipality.Text = dgvMain.Item(42, e.RowIndex).Value
            txtProvince.Text = dgvMain.Item(43, e.RowIndex).Value
         Dim ms As New MemoryStream(changephoto(CInt(dgvMain.SelectedCells(0).Value)))
            PictureBox1.Image = Image.FromStream(ms)

 Catch ex As Exception

        End Try

    End Sub
4

2 回答 2

0

正如我在上面的评论中提到的,尝试检查您的值,如下所示;您可能还想投射这些单元格DataGridViewCheckBoxCell。此外,您可能在代码中抛出异常,请设置断点并单步执行以查看是否抛出异常,如果是,请发布该异常,以便我更好地为您提供帮助。

 If CType(dgvMain.Rows(e.RowIndex).Cells("your column"), DataGridViewCheckBoxCell).Value Then
   'Do your work if it's true
 Else
   'Do something else...
 End If
于 2014-11-17T11:58:25.850 回答
0

我在 Value 之后添加了 ToString,它成功了。

If dgvMain.Item(35, e.RowIndex).Value.ToString = "True" Then
clbUnemployed.SetItemCheckState(0, CheckState.Checked)
Else
clbUnemployed.SetItemCheckState(0, CheckState.Unchecked)
End If

...但现在我无法在 PictureBox1 上显示照片。问题似乎源于这里(您也可以查看上面的代码):

Dim ms As New MemoryStream(changephoto(CInt(dgvMain.SelectedCells(0).Value)))
PictureBox1.Image = Image.FromStream(ms)

我用这个功能来做到这一点:

Function changephoto(ByVal photo As Integer) As Byte()
        sqlCon.Open()
        With sqlComm
            .Connection = sqlCon
            .CommandText = "SELECT Photo FROM Picture WHERE PersonId =" & dgvMain.SelectedRows(0).Cells(0).Value
        End With
        Dim myphoto() As Byte = CType(sqlComm.ExecuteScalar(), Byte())
        sqlCon.Close()
        Return myphoto
    End Function

错误说:从类型“数据类型(这取决于我单击的单元格)”到类型“整数”的转换无效。

于 2014-11-20T07:21:46.870 回答