1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
'La classe ListBoxColumn
Imports System.ComponentModel
Imports System.Windows.Forms.ListBox
Imports System.Drawing.Design
Public Class ListBoxColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.new(New ListBoxCell)
End Sub
Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As System.Windows.Forms.DataGridViewCell)
If (value IsNot Nothing And Not value.GetType().IsAssignableFrom(GetType(ListBoxCell))) Then
Throw New InvalidCastException("Must be a ListBoxCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Public Class ListBoxCell
Inherits DataGridViewCell
Public Sub New()
MyBase.New()
End Sub
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
'Set the value of the editing control to the current cell value.
Dim ctl As ListBoxEditingControl = _
CType(DataGridView.EditingControl, ListBoxEditingControl)
InitializeListBox(ctl, CType(Me.FormattedValue, ICollection))
End Sub
Private Sub InitializeListBox(ByVal ctrl As ListBox, ByVal value As ICollection)
ctrl.Items.Clear()
For Each obj As Object In value
ctrl.Items.Add(obj.ToString())
Next
ctrl.Tag = Me.Value
End Sub
Public Overrides ReadOnly Property EditType() As System.Type
Get
'Return MyBase.EditType
Return GetType(TestListBoxEditingControl)
End Get
End Property
Protected Overrides Function GetFormattedValue(ByVal value As Object, ByVal rowIndex As Integer, ByRef cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal valueTypeConverter As System.ComponentModel.TypeConverter, ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter, ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object
If (value Is Nothing) Then
Return New List(Of Object)
End If
Return MyBase.GetFormattedValue(value, rowIndex, cellStyle, valueTypeConverter, formattedValueTypeConverter, context)
End Function
Public Overrides ReadOnly Property FormattedValueType() As System.Type
Get
'Return MyBase.FormattedValueType
Return GetType(ICollection)
End Get
End Property
Public Overrides Property ValueType() As System.Type
Get
'Return MyBase.ValueType
Return GetType(ICollection)
End Get
Set(ByVal value As System.Type)
End Set
End Property
'In order to display ListBox in a DataGridView cell,
'you need to paint the cell by yourself
Private internalControl As ListBox
Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal cellState As System.Windows.Forms.DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
graphics.FillRectangle(New SolidBrush(cellStyle.BackColor), cellBounds)
If (internalControl Is Nothing) Then
internalControl = New ListBox()
End If
internalControl.Items.Clear()
Dim collection As ICollection = CType(value, ICollection)
If (collection IsNot Nothing) Then
For Each obj As Object In collection
internalControl.Items.Add(obj)
Next
Dim bmp As Bitmap = New Bitmap(cellBounds.Width, cellBounds.Height)
internalControl.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
graphics.DrawImage(bmp, cellBounds, New Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel)
End If
End Sub
Protected Overrides Sub OnClick(ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
Me.DataGridView.BeginEdit(False)
MyBase.OnClick(e)
End Sub
End Class
Public Class ListBoxEditingControl
Inherits ListBox
Implements IDataGridViewEditingControl
Private dataGridView As DataGridView
Private valueChanged As Boolean = False
Private rowIndex As Integer
Public Sub New()
End Sub
'Implements the IDataGridViewEditingControl.ApplyCellStyleToEditingControl
'method.
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle) Implements System.Windows.Forms.IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.Font = dataGridViewCellStyle.Font
Me.ForeColor = dataGridViewCellStyle.ForeColor
Me.BackColor = dataGridViewCellStyle.BackColor
End Sub
'Implements the IDataGridViewEditingControl.EditingControlDataGridView
'property.
Public Property EditingControlDataGridView() As System.Windows.Forms.DataGridView Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView
Get
Return dataGridView
End Get
Set(ByVal value As System.Windows.Forms.DataGridView)
dataGridView = value
End Set
End Property
'Implements the IDataGridViewEditingControl.EditingControlFormattedValue
' property.
Public Property EditingControlFormattedValue() As Object Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return Me.Tag
End Get
Set(ByVal value As Object)
' Me.Tag = value;
End Set
End Property
'Implements the IDataGridViewEditingControl.EditingControlRowIndex
'property.
Public Property EditingControlRowIndex() As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
Get
Return rowIndex
End Get
Set(ByVal value As Integer)
rowIndex = value
End Set
End Property
'Implements the IDataGridViewEditingControl.EditingControlValueChanged
'property
Public Property EditingControlValueChanged() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
Get
Return valueChanged
End Get
Set(ByVal value As Boolean)
valueChanged = value
End Set
End Property
'Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
'method.
Public Function EditingControlWantsInputKey(ByVal keyData As System.Windows.Forms.Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlWantsInputKey
'Let the DateTimePicker handle the keys listed.
Select Case keyData And Keys.KeyCode
Case Keys.Left, Keys.Up, Keys.Down, _
Keys.Right, Keys.Home, Keys.End, _
Keys.PageDown, Keys.PageUp
Return True
Case Else
Return Not dataGridViewWantsInputKey
End Select
End Function
'Implements the IDataGridViewEditingControl
'.EditingPanelCursor property.
Public ReadOnly Property EditingPanelCursor() As System.Windows.Forms.Cursor Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property
'Implements the IDataGridViewEditingControl.GetEditingControlFormattedValue
'method.
Public Function GetEditingControlFormattedValue(ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
Return EditingControlFormattedValue
End Function
' Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
'method.
Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
'No preparation needs to be done.
End Sub
'Implements the IDataGridViewEditingControl.RepositionEditingControlOnValueChange
'property.
Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
Return False
End Get
End Property
End Class
' Le Form d'utilisation avec un simple DataGridView et un TextBox en plus.
' et gestion de l'evenement DataGridView_EditingControlShowing et
' interaction avec notre ListBoxColumn
'-------------------------------------------------------------
'
'utilisation avec un datatable auxiliaire compose de 2
'champs:
'simple => "NomChamp"
'complexe => "ListeValeur" (List(Of String)
Public Class frmListBoxColum
'les valeurs du tableau row sont envoyes dans le DataTable
Dim dt As DataTable
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
Me.Text = "DataGridView ListBoxColumn"
End Sub
Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) _
Handles Me.Load
'1/DATATABLE
'Creation des 2 champs
dt = New DataTable
dt.TableName = "dt"
dt.Columns.Add("NomChamp", GetType(String))
dt.Columns.Add("ListeValeur", GetType(List(Of String)))
'Remplir le champ complexe "ListeValeur"
Dim nbValeur As Integer = 4
Dim simpleListe As List(Of String) = New List(Of String)
For j As Integer = 0 To nbValeur
simpleListe.Add("Valeur" & (j + 1).ToString)
Next
'Remplit le datatable avec les 2 champs simple et complexe
Dim dr As DataRow = dt.NewRow
For i As Integer = 0 To 9
dr(0) = "NomChamp" & (i + 1).ToString
dr(1) = simpleListe
dt.Rows.Add(dr)
dr = dt.NewRow
Next
'2/DataGridView
'Ajoute notre colonne ListBoxColumn(visible dans Designer
'si la classe ListBoxColumn est dans le meme projet)
Dim col As ListBoxColumn = New ListBoxColumn
col.HeaderText = "Liste des Valeurs"
Me.DataGridViewDT.Columns.Add(col)
'binder la colonne au champ "ListeValeur" de la DataTable
Me.DataGridViewDT.Columns(0).DataPropertyName = "ListeValeur"
'Binder le DataGridView à la DataTable
Me.DataGridViewDT.DataSource = dt
End Sub
'L'exemple est calque sur le code MSDN (cite-apres).Il montre
'comment utiliser un gestionnaire d'événements DataGridView.EditingControlShowing
'pour ajouter un gestionnaire pour un événement
'de la classe encapsule ListBoxEditingControl.
'Dans l'exemple, le contrôle d'édition est casté en un ListBox pour gérer
'l'événement ListBox.SelectedIndexChanged.
'(voir exemple MSDN ComboBox )
Private Sub DataGridViewDT_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridViewDT.EditingControlShowing
Dim objListBox As ListBox = CType(e.Control, ListBox)
If (objListBox IsNot Nothing) Then
' Remove an existing event-handler, if present, to avoid
' adding multiple handlers when the editing control is reused.
RemoveHandler objListBox.SelectedIndexChanged, _
New EventHandler(AddressOf ListBox_SelectedIndexChanged)
' Add the event handler.
AddHandler objListBox.SelectedIndexChanged, _
New EventHandler(AddressOf ListBox_SelectedIndexChanged)
End If
End Sub
'handler ListBox_SelectedIndexChanged
Private Sub ListBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim objListBox1 As ListBox = CType(sender, ListBox)
' Get the currently selected item in the ListBoxColumn.
Dim curItem As String = objListBox1.SelectedItem.ToString
Dim index As Integer = objListBox1.SelectedIndex
If index = -1 Then
MessageBox.Show("Item is not available in ListBoxColumn")
Else
Me.TextBox1.Text = curItem
Me.TextBox1.ForeColor = Drawing.Color.Red
End If
End Sub
End Class |
Partager