开发者

Capturing value of DataGridView CheckBox in VB.Net

开发者 https://www.devze.com 2023-01-10 15:19 出处:网络
I have a datagridview (unbound). Fields are Name, Family Name and Phone No开发者_JAVA百科 and a checkbox colum.

I have a datagridview (unbound). Fields are Name, Family Name and Phone No开发者_JAVA百科 and a checkbox colum.

There are ten rows in that DataGridView.

There is an OK button

I need to get message of showing which rows user has checked. The message should appear when user clicks on the OK button. There could be several messages, checking each row one by one, in a loop.

I am not able to get this message. I tried following code in OK button :

Dim strCB As String = dgvChooseQs.Rows(0).Cells(3).Value.ToString

Cell(3) is my checkbox. Do not consider Rows(0), at the moment I am just checking value at row 0

Thanks for your help.

Furqan


Do not use the cell index. Your checkbox column must have a name so you should use it.

Otherwise, what you want to do would be something like this


For each oRow as DataGridViewRow in dgvChooseQs.Rows

   If oRow.Cells("ColNamE").Value = True then
    'do whatever you need to do.
   End if

Next

If you feel you need to cast the column, then you can use CType, but the type is DataGridViewCheckBoxCell, not CheckBox.


You need to do something like this:

if ctype(dgvChooseQs.Rows(0).findcontrol("whateverYourCheckBoxIsNamed"), checkbox).checked then 

'throw the message

end if


You may cast the cell value to Boolean, and then check it, as follows:

Dim RowIndex As Integer = ...
Dim ColumnIndex As Integer = ...

Dim IsTicked As Boolean = CBool(DataGridView1.Rows(RowIndex).Cells(ColumnIndex).Value)

If IsTicked Then
    MessageBox.Show("You ticked the box.")
Else
    MessageBox.Show("You cleared the box.")
End If


Only the third example worked for me but I had to add a Timer

Private Sub DgvElencoFile_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DgvElencoFile.CellClick

    'Variabili gestione programma
    Dim numerocolonnaSelezionata As Integer
    Dim numeroColonnaSelezionataPerDowonload As Integer
    Dim numeroRigaSelezionata As Integer

    numeroRigaSelezionata = e.RowIndex
    NumerocolonnaSelezionata = e.ColumnIndex

    If NumeroRigaSelezionata < 0 Then
        ClaFunzSgnSon.SegnaleSonoro(ClaFunzSgnSon.EnTipoSgnSon.ErroreImpostazioneDati)
        GoTo FineSubFunz
    End If
    numeroColonnaSelezionataPerDowonload = -1

    If DgvElencoFile.Rows(numeroRigaSelezionata).Cells(ClnDownLoad.Name).Selected Then numeroColonnaSelezionataPerDowonload = numerocolonnaSelezionata

    If numeroColonnaSelezionataPerDowonload >= 0 Then
        TimVisCheckDownLoad.Start()
    End If

FineSubFunz: End Sub

Private Sub TimVisCheckDownLoad_Tick(sender As Object, e As EventArgs) Handles TimVisCheckDownLoad.Tick TimVisCheckDownLoad.Stop()

    Dim isTickedOn = CBool(DirectCast(DgvElencoFile.CurrentCell, DataGridViewCheckBoxCell).EditingCellFormattedValue)

    If isTickedOn Then
        MessageBox.Show("You ticked the box.")
    Else
        MessageBox.Show("You cleared the box.")
    End If

End Sub


I found a simple solution.

Just change the cell focus after click on cell.

Private Sub DGV_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellContentClick
    If e.ColumnIndex = "Here checkbox column id or name" Then
        DGV.Item(e.ColumnIndex, e.RowIndex + 1).Selected = True
        'Here your code
        MsgBox DGV.Item(e.ColumnIndex, e.RowIndex).Value
    End If
End Sub

Don't forget to check if the column of your (ckeckbox + 1) index exist.


Set type of grid in dataGridView to 'DataGridViewCheckBoxXColumn' instead of DataGridViewCheckBoxColumn.

all problems will be solved


The post is old but it can help in need: You have these three properties after casting your cell:

Private Sub YourDataGridView_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles YourDataGridView.CellMouseClick

    Dim b, b1, b2 As Boolean
    b = DirectCast(YourDataGridView.CurrentCell, DataGridViewCheckBoxCell).EditingCellValueChanged
    b1 = CBool(DirectCast(YourDataGridView.CurrentCell, DataGridViewCheckBoxCell).EditingCellFormattedValue)
    b2 = CBool(DirectCast(YourDataGridView.CurrentCell, DataGridViewCheckBoxCell).EditedFormattedValue)
End Sub


it's a long time since this question was sent, but can be useful this answer to anybody with the same issue. In my case, I used (I'm using your notation):

dgvChooseQs.Item(6, vCont).State

where 6 is the checkbox column number, in my case column 6. vCont is an iteration counter in a FOR NEXT loop. If State equals 32, checkbox was checked, else State will be zero. I hope this can be of help.

0

精彩评论

暂无评论...
验证码 换一张
取 消