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) .Value – 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 '형식에서'Boolean '형식으로 변환이 유효하지 않습니다.' – Irond64dpool

0

ToString after Value를 추가하고 트릭을 수행했습니다.

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

...하지만 지금은 PictureBox1에 사진을 표시하는 데 문제가 있습니다.

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 

오류가 말한다 :

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

내가 할이 기능을 사용 : 문제는 (당신은 또한 위의 코드를 볼 수 있습니다) 여기에서 발생하는 것으로 보인다 형식에서 변환을 '데이터 형식 (이 '클릭'셀에 따라 다름) 'Integer'를 입력하면 유효하지 않습니다.