Voici une modification d'une source que j'ai trouvé sur ce site et qui permet de créer une ComboBoxColumn pour un DataGrid et qui marche pour une table source à une seule colonne.
Pour l'utiliser, il faut écrire, dans le code de création du DataGrid le code suivant :
'Ajout de la comboboxcolumn au DataGrid1
Dim DataComboBoxColumn As New DataGridComboBoxColumn(Table_source_de_la_combobox)
DataComboBoxColumn.MappingName = "Nom_de_la_colonne_liée"
DataComboBoxColumn.HeaderText = "Texte_de_l'entête_de_colonne"
DataComboBoxColumn.NullText = ""
DataComboBoxColumn.Width = largeur de la colonne
DataGrid1.DataGridTableStyle.GridColumnStyles.Add(DataComboBoxColumn)
Source / Exemple :
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel
Public Class DataGridComboBoxColumn
Inherits System.Windows.Forms.DataGridTextBoxColumn
Dim Combo As New ComboBox
Private DG As New DataGrid
Private DT As New DataTable
Private varTable As DataTable
Private IsEditing As Boolean
#Region " Code généré par le Concepteur Windows Form "
Public Sub New(ByVal DTtype As DataTable, ByVal DGtype As DataGrid)
MyBase.New()
DT = DTtype
DG = DGtype
'Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
'Ajoutez une initialisation quelconque après l'appel InitializeComponent()
End Sub
'La méthode substituée Dispose du UserControl pour nettoyer la liste des composants.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Requis par le Concepteur Windows Form
Private components As System.ComponentModel.IContainer
'REMARQUE : la procédure suivante est requise par le Concepteur Windows Form
'Elle peut être modifiée en utilisant le Concepteur Windows Form.
'Ne la modifiez pas en utilisant l'éditeur de code.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'DataGridComboBoxColumn
'
components = New System.ComponentModel.Container
Combo.DataSource = DT
Combo.DisplayMember = DT.Columns(0).ToString
Combo.ValueMember = DT.Columns(0).ToString
Combo.Visible = False
IsEditing = False
End Sub
#End Region
Protected Overrides Sub Abort(ByVal rowNum As Integer)
End Sub
'Enregistre la valeur sélectionnée dans la table liée et rend la comboBox invisible
Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean
If IsEditing = True Then
SetColumnValueAtRow(dataSource, rowNum, Me.Combo.SelectedValue)
Combo.Visible = False
IsEditing = False
End If
Return True
End Function
Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
Try
If cellIsVisible Then
Me.Combo.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height)
Me.Combo.SelectedIndex = 0
Me.Combo.Visible = True
Me.Combo.SelectedValue = GetColumnValueAtRow(source, rowNum)
End If
Catch ex As Exception
Me.Combo.SelectedIndex = 0
End Try
IsEditing = True
End Sub
'Affiche la valeur dans la cellule du DataGrid
Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
Try
Dim clef As Object
Dim valeur As String
Dim rect As Rectangle = bounds
Dim rectF As RectangleF
Dim strFormat As New StringFormat
clef = GetColumnValueAtRow(source, rowNum)
If clef Is DBNull.Value OrElse clef Is Nothing Then
g.FillRectangle(backBrush, rect)
Exit Sub
Else
With strFormat
If alignToRight Then
.Alignment = StringAlignment.Far
Else
Select Case Me.Alignment
Case HorizontalAlignment.Center
.Alignment = StringAlignment.Center
Case HorizontalAlignment.Left
.Alignment = StringAlignment.Near
Case Else
.Alignment = StringAlignment.Far
End Select
.FormatFlags = .FormatFlags Or StringFormatFlags.NoWrap
End If
End With
valeur = Combo.SelectedValue
g.FillRectangle(backBrush, rect)
rect.Offset(0, 2)
rect.Height -= 2
Paint(g, bounds, source, rowNum, False)
End If
Catch ex As Exception
MessageBox.Show(ex.ToString, "Erreur d'Affichage des données")
End Try
End Sub
Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid)
value.Controls.Add(Combo)
End Sub
End Class
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.