0

我目前正在使用VB.NET和SQL Server的數據庫系統,除了我的CheckListBox和其他對象之後,一切正常。通過DataGridView顯示數值到清單框CellClick事件

我創建了一個事件,當我點擊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 
+0

您拋出異常。你可以設置一個斷點,並通過... – Codexer

+0

如果我還記得以及檢查單元格值應該是:dgvMain.Rows(e.RowIndex).Cells(yourcolumn)。值 – Codexer

+0

謝謝!其實我從來沒有嘗試過,因爲我還是從一個新的程序員開始,只從例子中學習。我會看看我能否找到問題併發布解決方案。我真的很感謝你的建議。 – Irond64dpool

回答

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 
+0

我嘗試了上面的代碼,但它給了我這個錯誤: 'InvalidCastException未處理' '從類型'DBNull'轉換爲類型'布爾'無效' – Irond64dpool

0

我在值後添加了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 

錯誤說:從類型轉換「的數據類型(此取決於我單擊的單元格)'鍵入'整數'無效。

相關問題