I have a question regarding DataGridView
control in .NET.
I inserted a DataGridView
from the toolbox and I connected it with a database that I setup in access. Then I added a column with buttons from the edit columns of the DataGridView
tasks panel.
The click events of the DataGridView
buttons work without a problem!
I want to perform a click on DataGridView
button programmatically when I click another button outside of the DataGridView
. How should I do this?
The code of the DataGridView is:
Private Sub dgvAnimSel_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) _
Handles dgvAnimSel.CellContentClick
Dim V As String = dgvAnimSel.Rows(e.RowIndex).Cells(0).Value
If e.ColumnIndex = 3 Then
If V = 1 Then
If A1 = 1 Then
'this is the uncheck state
Me.dgvAnimSel.CurrentCell.Style.BackColor = Color.White
Me.dgvAnimSel.CurrentCell.Style.ForeColor = Color.Black
Me.dgvAnimSel.CurrentCell.Value = "Select"
ItemTextNew = ItemTextOr + "1"
ItemName = ListView1.FindItemWithText(ItemTextNew, False, 0, True)
ListView1.Items.Remove(ItemName)
A1 = 0
Else
'this is the check state
Me.dgvAnimSel.CurrentCell.Style.BackColor = Color.Green
Me.dgvAnimSel.CurrentCell.Style.ForeColor = Color.White
Me.dgvAnimSel.CurrentCell.Value = "Selected"
a = ListView1.Items.Add(" " + "Animation 1 ", 0)
A1 = 1
End If
End If
End Sub
Thank you in advance!
You can use either of the following options:
CellContentClick
like a normal method by creating an instance ofDataGridViewCellEventArgs
and pass it to the event handler method.CellContentClick
of theDataGridView
orClick
of the button.VB.NET
Example 1 - Perform Click for DataGrdiView Button Cell by calling the event handler
To programmatically click on button in specific row, you can call the method that you created as event handler of
CellContentClick
event, using suitableDataGridViewCellEventArgs
ase
and yourDataGridView
assender
:Example 2 - Putting the logic in another method and call the method when you need
As another option you can put the logic related to click on a cell button in a method, dependent from
Cell
andRow
objects and only pass suitable values to that method. Then you can call the method wherever you need.C#
Example 1 - Perform Click for DataGrdiView Button Cell by calling the event handler
To programmatically click on button in specific row, you can call the method that you created as event handler of
CellContentClick
event, using suitableDataGridViewCellEventArgs
ase
and yourDataGridView
assender
:Example 2 - Putting the logic in another method and call the method when you need
As another option you can put the logic related to click on a cell button in a method, dependent from
Cell
andRow
objects and only pass suitable values to that method. Then you can call the method wherever you need.If you want to generate programmatically click on
DataGridViewButtonCell
instance, you can use DataGridViewCell.AccessibilityObject property and call DoDefaultAction method.Something like this (sorry for C#, I'm sure you can translate it to VB):
Test: