monDataGridView.Rows(X).ReadOnly = True
'Création du style pour les lignes non autorisées Dim unselectedStyle As DataGridViewCellStyle = Me.DataGridView1.DefaultCellStyle.Clone() unselectedStyle.SelectionBackColor = unselectedStype.BackColor unselectedStyle.SelectionForeColor = unselectedStype.ForeColor
'Bloque la sélection des lignes 2 et 4 Me.DataGridView1.Rows(1).DefaultCellStyle = unselectedStyle Me.DataGridView1.Rows(3).DefaultCellStyle = unselectedStyle
'Pour chaque lignes sélectionnées For Each ligne As DataGridViewRow In Me.DataGridView1.SelectedRows 'Si la ligne n'a pas de style par défaut ou que le tag du style 'n'est pas "disabled" c'est que la ligne est autorisée If (Not ligne.HasDefaultCellStyle) OrElse _ (CType(ligne.DefaultCellStyle.Tag, String) <> "disabled") Then Console.WriteLine("La ligne {0} est sélectionnée", ligne.Index + 1) End If Next
Me.DataGridView1.Rows(1).DefaultCellStyle = Nothing
Public Class DataGridViewRowCustom Inherits DataGridViewRow Private _enabled As Boolean Public Property Enabled As Boolean Get Return _enabled End Get Set(ByVal value As Boolean) _enabled = value If (Me.DataGridView IsNot Nothing) Then Me.DataGridView.Refresh() End Set End Property Public Sub New() MyBase.New() _enabled = True End Sub Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal rowBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal rowState As System.Windows.Forms.DataGridViewElementStates, ByVal isFirstDisplayedRow As Boolean, ByVal isLastVisibleRow As Boolean) 'Test si l'état de la ligne contient l'état sélectionné et que la sélection est interdite If (((rowState And DataGridViewElementStates.Selected) DataGridViewElementStates.Selected) And (_enabled False)) Then 'On supprime l'état sélectionné de l'état actuel de la ligne rowState = (rowState Xor DataGridViewElementStates.Selected) End If MyBase.Paint(graphics, clipBounds, rowBounds, rowIndex, rowState, isFirstDisplayedRow, isLastVisibleRow) End Sub End Class
Me.DataGridView1.Rows.Add(New DataGridViewRowCustom())
CType(Me.DataGridView1.Rows(1), DataGridViewRowCustom).Enabled = False
For Each ligne As DataGridViewRow In Me.DataGridView1.SelectedRows Dim custRow = TryCast(ligne, DataGridViewRowCustom) 'Test si la ligne est bien une ligne custom (car si vous autorisez l'ajout, 'la ligne d'ajout n'est pas une DataGridViewRowCustom) 'et si la ligne est autorisée If (custRow IsNot Nothing) AndAlso custRow.Enabled Then Console.WriteLine("La ligne {0} est sélectionnée", ligne.Index + 1) End If Next
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPublic Class DataGridViewRowCustom Inherits DataGridViewRow Private _enabled As Boolean Public Property Enabled As Boolean Get Return _enabled End Get Set(ByVal value As Boolean) _enabled = value 'Si la ligne était sélectionnée et qu'elle passe bloquée on supprime la ligne de la sélection If (Me.Selected And (value False)) Then Me.Selected False End Set End Property Public Sub New() MyBase.New() _enabled = True End Sub Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal rowBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal rowState As System.Windows.Forms.DataGridViewElementStates, ByVal isFirstDisplayedRow As Boolean, ByVal isLastVisibleRow As Boolean) 'Test si l'état de la ligne contient l'état sélectionné et que la sélection est interdite If (((rowState And DataGridViewElementStates.Selected) DataGridViewElementStates.Selected) And (_enabled False)) Then 'On supprime l'état sélectionné de l'état actuel de la ligne rowState = (rowState Xor DataGridViewElementStates.Selected) 'On supprime la ligne de la sélection Me.Selected = False End If MyBase.Paint(graphics, clipBounds, rowBounds, rowIndex, rowState, isFirstDisplayedRow, isLastVisibleRow) End Sub End Class
For Each ligne As DataGridViewRow In Me.DataGridView1.SelectedRows Console.WriteLine("La ligne {0} est sélectionnée", ligne.Index + 1) Next