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.
What is wrong with those code?
Thank you.
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
What is wrong with those code?
Thank you.
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.
Why use CellValueChanged instead of Click ?
Thank you.
ASKER
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.
Thank you.
ASKER
I change to CellValueChanged, but the event does not triggered.
Very weird.
Thank you.
Very weird.
Thank you.
PLease post code for Get_Column_Name_From_Datag ridview function
ASKER
Hi Partha,
The function is not problem. This function is used by all forms of many application.
Thank you.
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 ""
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Saige,
Dim grid = CType(sender, DataGridView)
Why you need to cast it again, why not using DataGridView1 instead?
Thank you.
Dim grid = CType(sender, DataGridView)
Why you need to cast it again, why not using DataGridView1 instead?
Thank you.
ASKER
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.
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-
As for your second question, you need to move to the next cell itself. Agreed, this is poor design on Microsofts part.
-saige-
ASKER
Hi Saige,
Thank you very much for your help.
Thank you very much for your help.
Try moving your code to the CellValueChanged (or possibly CellValueChanging) event instead of the CellClick.