What event handler is best in using Data Grid View fire on Click in VB.net? - vb.net

I'm having a problem with DataGridView event handler CellContentClick sometimes it does not work.
here is my code:
Private Sub DGV_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellContentClick
ownDataSelected()
End Sub
Please help me. Thankyou

Use DataGridView.CellClick
This event occurs when any part of a cell is clicked, including
borders and padding. It also occurs when the user presses and releases
the SPACE key while a button cell or check box cell has focus, and
will occur twice for these cell types if the cell is clicked while
pressing the SPACE key.
DataGridView.CellContentClick triggers only on the text of the cell being click and the blank space not count.

Related

DataGridView resets to top when clicked after mouse scroll

I have a DataGridView control on a TabPage of a Windows Form application.
When the user moves the mouse over the DataGrid and uses the scroll wheel, the grid scrolls as expected. But when the user clicks in a cell on the screen, instead of the cell receiving focus, the DataGrid resets to the top and requires the user to scroll down again. This response is non-intuitive since it's not immediately obvious that the cell you thought you clicked on isn't there anymore.
I would be happy to prevent the DataGrid from responding to the scroll wheel until the user clicks in the grid, or preferably to maintain the current actions except not resetting to the top when first clicked.
From what I've researched here, it appears that the DataGrid is rebinding because I'm resetting the binding when the tabpage is entered (since the database might have been updated by one of the other tabs.
Private Sub TabPage1_Enter(sender As Object, e As EventArgs) Handles TabPage1.Enter
LoadTACTable()
End Sub
In LoadTACTable():
dbGetList("spSelectTACList", dtTACs, 0, 100000, Nothing) ' Record numbers are 0 based
bsTACs.DataSource = dtTACs
With gridTACs
' TOTAL Grid width = 1380
.DataSource() = bsTACs
.
.
.
(Showing only part of the code for brevity.
Is there a way to see if the TabPage is already displayed when entered? Or, is unnecessary to reset the gridTAC datasource every time I retrieve the data from the SQL database to the dtTACs datatable using my dbGetList() sub?
There are several possible solutions to your problem. One would be to not automatically rebind the datagrid but let the user do it by clicking some refresh button. That way the user would not see non-intuitive behavior.
You mentioned that the contents of one tab may need to be refreshed when the contents of other tabs are changed. Whenever the contents of a tab is changed and can affect other tabs, you could flag these other tabs (for example, by adding a star to their titles) to indicate that they no longer have the latest data. The user would then know that the tab needs to be refreshed.
There might be other solutions, but it is difficult to tell without knowing more about your use case.
With the guidance above, I believe I solved the issue:
I created a flag:
Dim TabDirty As Boolean
Then I set it in the TabPage.Leave handler:
Private Sub TabPage1_Leave(sender As Object, e As EventArgs) Handles TabPage1.Leave
dtTACs.Dispose()
TabDirty = True
End Sub
Then I just check it when I enter the TabPage:
Private Sub TabPage1_Enter(sender As Object, e As EventArgs) Handles TabPage1.Enter
If TabDirty = True Then
TabDirty = False
LoadTACTable()
End If
End Sub
So far, this appears to work - the grid is not getting reset when clicked, but I will do a bit more testing to confirm that the data is refreshed when necessary.

Check If User Pressed Enter to leave DataGridViewCell

I am working in VB.Net programming user functionality into a DataGridView. Currently, I am trying to allow users to update the data that is being shown in the DataGridView by editting what is in the Cells.
Right now I have a method to detect when the user begins the edit the cell:
Private Sub dataTable_CellBeginEdit(ByVal sender As Object, ByVal e As DataGridViewCellCancelEventArgs) Handles dataTable.CellBeginEdit
oldCellVal = dataTable.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
End Sub
The point of the method is to store the old data in the cell in memory so that it can be restored later if the user decides not to edit the cell.
Now I am not sure how to set up the method for when the user completes the edit. I know there is a CellEndEdit event I can make a Handler for, but in that method how would I detect how they left the Cell?
I'd like to set it up so that if my User presses the Enter key, only then is their edit submitted. If they use the Arrow Keys, or the ESC key to exit the Cell, they instead get a "Would you like to stop editting?" prompt. This makes it sound like I need a keypress event, but if I do that kind of event, how do I properly detect the Cell that was modified? I need the know the row and column index of the updated Cell in order to properly submit the changes.
So how should I go about doing this? KeyPress or CellEndEdit? Or is there something else I haven't considered?

Button Click vs ShortCut Key (ALT) Event firing order

I have a Save button on a form with a shortcut key set (ALT+S).
In my datagrid in the cellvalidated event I make a determination if I can enable or disable this button for the users.
Similar to the old VB6 problem of Event firing order....
If the button was currently enabled, but user puts invalid data in the datagrid cell. If they Click on the button, the cell validated event fires, disables the button - no issue. When, instead, they input the bad data, and do ALT+S, the cell validated event fires, but in this case the Button_Click event still fires.
Any way to prevent this? Basically if the use the ALT+, I don't want the Click event to fire.
If everything else fails - there is this method (in pseudo-code)
Class Form
private _validated as Boolean
sub Cell_validate
' validate here and set
_validated = true/false
end sub
sub Button_Click
If not _validated then Return
' have your save logic here
end sub
End class
This way you will protect your code from executing actual logic of Button_Click when button disabled.

error on cell value change event in vbnet datagridview

I simply added message box and when i run the form the message box keeps popping up until all the rows have finish loaded n gridview.
My initial plan was to show a message that you have edited the cell. So does cell change event happens when the gridview is being loaded and extracting the rows from the database or when you change the value of cell.
How to stop the message box from popping out countless of times and whether or not i am using the wrong event?? Below is exactly what i did. I am also using datasource to get my records from the database
Private Sub grdDataGrid_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles grdDataGrid.CellValueChanged
MsgBox("You have edited the follwing cell")
End Sub
To stop the messageBox from popping out countless times:
Private Sub grdDataGrid_DataBindingComplete(sender As Object, e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles grdDataGrid.DataBindingComplete
MessageBox("your message")
End Sub
When you use the _CellValueChanged event- all of the cells that are being loaded with data are being changed when you render the gridView. So each cell is firing the _CellValueChanged event

Showing List of combobox while getting focus (vb.net)

When we click on drop down combobox control in our windows form, it shows the list automatically.
But when we press tab and navigate to that control from keyboard it doesn't shows the list automatically. So in other to show the list automatically on receiving focus what should be done?
Set the DroppedDown property of the combobox equal to true.
Private Sub myComboBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles myComboBox.GotFocus
myComboBox.DroppedDown= true
End Sub
I would like to mention on thing here.
I used Enter event to show Drop down list with DroppedDown=true,
But When I type something in text area of combobox and if i leave the area to next control, entered text is lost.
My combobox is databound.