DataGridView.CellContentClick Event
Occurs when the content within a cell is clicked.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
This event occurs when the cell content is clicked. It also occurs when the user presses and releases the SPACEBAR while a button cell or check box cell has focus, and will occur twice for these cell types if the cell content is clicked while pressing the SPACEBAR.
Use this event to detect button clicks for a DataGridViewButtonCell or link clicks for a DataGridViewLinkCell.
For clicks in a DataGridViewCheckBoxCell, this event occurs before the check box changes value, so if you do not want to calculate the expected value based on the current value, you will typically handle the DataGridView.CellValueChanged event instead. Because that event occurs only when the user-specified value is committed, which typically occurs when focus leaves the cell, you must also handle the DataGridView.CurrentCellDirtyStateChanged event. In that handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.
For more information about handling events, see NIB: Consuming Events.
The following code example provides a handler for this event that determines whether the clicked cell is a link cell or a button cell and performs the corresponding action as a result. This example is part of a larger example available in the DataGridViewComboBoxColumn class overview topic.
Private Sub DataGridView1_CellContentClick(ByVal sender As Object, _ ByVal e As DataGridViewCellEventArgs) _ Handles DataGridView1.CellContentClick If IsANonHeaderLinkCell(e) Then MoveToLinked(e) ElseIf IsANonHeaderButtonCell(e) Then PopulateSales(e) End If End Sub Private Sub MoveToLinked(ByVal e As DataGridViewCellEventArgs) Dim employeeId As String Dim value As Object = DataGridView1.Rows(e.RowIndex). _ Cells(e.ColumnIndex).Value If value.GetType Is GetType(DBNull) Then Return employeeId = CType(value, String) Dim boss As DataGridViewCell = _ RetrieveSuperiorsLastNameCell(employeeId) If boss IsNot Nothing Then DataGridView1.CurrentCell = boss End If End Sub Private Function IsANonHeaderLinkCell(ByVal cellEvent As _ DataGridViewCellEventArgs) As Boolean If TypeOf DataGridView1.Columns(cellEvent.ColumnIndex) _ Is DataGridViewLinkColumn _ AndAlso Not cellEvent.RowIndex = -1 Then _ Return True Else Return False End Function Private Function IsANonHeaderButtonCell(ByVal cellEvent As _ DataGridViewCellEventArgs) As Boolean If TypeOf DataGridView1.Columns(cellEvent.ColumnIndex) _ Is DataGridViewButtonColumn _ AndAlso Not cellEvent.RowIndex = -1 Then _ Return True Else Return (False) End Function Private Function RetrieveSuperiorsLastNameCell( _ ByVal employeeId As String) As DataGridViewCell For Each row As DataGridViewRow In DataGridView1.Rows If row.IsNewRow Then Return Nothing If row.Cells(ColumnName.EmployeeId.ToString()). _ Value.ToString().Equals(employeeId) Then Return row.Cells(ColumnName.LastName.ToString()) End If Next Return Nothing End Function
Available since 2.0