Link to home
Start Free TrialLog in
Avatar of emi_sastra
emi_sastra

asked on

Cell Click Event Code

Hi All,

I have problem with datagridview cell click event.
Please see below code. It does not work as I want to.

When column "Pilih" is checked then QtyPilih should be filled from "TotalStok".
When column "Pilih" is uncheck the QtyPilih should be set to 0.

 Private Sub dgvData_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvData.CellClick

        blnGridOnFocus = True

        With dgvData

            If e.RowIndex < 0 Then Exit Sub
            If e.ColumnIndex < 0 Then Exit Sub

            Dim dgvRow As DataGridViewRow = .Rows(.CurrentRow.Index)

            Dim strColumnName As String = ERV_Global.Get_Column_Name_From_Datagridview( _
                          dgvData, _
                          e.ColumnIndex)

            Select Case strColumnName

                Case "Pilih"

                    Dim blnPilih As Boolean = .Rows(e.RowIndex).Cells(e.ColumnIndex).Value

                    .Rows(e.RowIndex).Cells(e.ColumnIndex).Value = Not blnPilih

                    If blnPilih = False Then

                        If dblMaxQty > 0 Then
                            dgvRow.Cells("QtyPilih").Value = dblSisaQty

                            If dgvRow.Cells("TotalStok").Value > dblSisaQty Then
                                dgvRow.Cells("QtyPilih").Value = dblSisaQty
                            Else
                                dgvRow.Cells("QtyPilih").Value = dgvRow.Cells("TotalStok").Value
                            End If
                        Else
                            dgvRow.Cells("QtyPilih").Value = dgvRow.Cells("TotalStok").Value
                        End If
                    Else
                        dgvRow.Cells("QtyPilih").Value = 0
                    End If

                    Me.Count_Total()

                Case "QtyPilih"

                    .BeginEdit(True)

            End Select

            .ShowEditingIcon = False

        End With

    End Sub

Open in new window


What is wrong with those code?

Thank you.
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Please explain what you mean by "It does not work as I want to". We have no idea what is not working, or what you want.

Try moving your code to the CellValueChanged (or possibly CellValueChanging) event instead of the CellClick.
Avatar of emi_sastra
emi_sastra

ASKER

It does not work, means sometimes it is selected, but QtyPilih = 0 or vice versa. Sometimes it is correct.

Why use CellValueChanged  instead of Click ?

Thank you.
There is weird thing. Even I click out of the checked box, but still at the cell, it still run the code, but the checkbox does not change after I click it.

Thank you.
I change to CellValueChanged, but the event does not triggered.
Very weird.

Thank you.
PLease post code for Get_Column_Name_From_Datagridview function
Hi Partha,

The function is not problem. This function is used by all forms of many application.

 
 Dim strColumnName As String = ""

        Try

            strColumnName = dgvName.Columns(intColumn).Name

      
            Return strColumnName

        Catch ex As Exception

        End Try

        Return ""

Open in new window


Thank you.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Saige,

   Dim grid = CType(sender, DataGridView)
Why you need to cast it again, why not using DataGridView1 instead?

Thank you.
Try to click out of the box but still inside the cell. You will see that the value of the checkbox does not change.

Thank you.
@emi_sastra, because I am casting the sender instead of assuming that it is DataGridView1, this is because you could potentially have multiple DataGridView's on the same form.

As for your second question, you need to move to the next cell itself.  Agreed, this is poor design on Microsofts part.

-saige-
Hi Saige,

Thank you very much for your help.