Excuse moi, je t'avais pas donné la classe, tu la trouveras ci dessous, mais attention, elle est pas du tout commentée. Je n'ai ni le temps, ni l'envie de la commenter.
Public Class DataGridComboBoxColumn Inherits DataGridTextBoxColumn
Private cmb As Combobox Private cm As CurrencyManager Private iCurrentRow As Integer
Public Sub New() Me.cm = Nothing
Me.cmb = New ComboBox Me.cmb.DropDownStyle = ComboBoxStyle.DropDownList Me.cmb.Sorted = True
AddHandler Me.cmb.Leave, AddressOf comboBox_Leave
End Sub
Public ReadOnly Property Combobox() As Combobox Get Combobox = cmb End Get End Property
Private Sub DataGrid_Scroll(ByVal sender As Object, ByVal e As EventArgs) Try Me.cmb.Hide()
Catch ex As Exception 'MsgBox(ex.ToString) End Try End Sub
Private Sub comboBox_Leave(ByVal sender As Object, ByVal e As EventArgs) Try Dim s As String Dim rowView As DataRowView = Me.cmb.SelectedItem If (Not rowView Is Nothing) Then s = rowView.Row(Me.cmb.DisplayMember) Else s = "" End If
SetColumnValueAtRow(Me.cm, Me.iCurrentRow, s) Invalidate()
Me.cmb.Hide() AddHandler Me.DataGridTableStyle.DataGrid.Scroll, AddressOf DataGrid_Scroll Catch ex As Exception 'MsgBox(ex.ToString) End Try
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal rOnly As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean) Try
MyBase.Edit(source, rowNum, bounds, rOnly, instantText, cellIsVisible)
If ((Not Me.ReadOnly) And (Not rOnly) And (cellIsVisible)) Then Me.iCurrentRow = rowNum Me.cm = source
AddHandler Me.DataGridTableStyle.DataGrid.Scroll, AddressOf DataGrid_Scroll
Me.cmb.Parent = Me.TextBox.Parent Dim rect As Rectangle = Me.DataGridTableStyle.DataGrid.GetCurrentCellBounds() Me.cmb.Location = rect.Location Me.cmb.Size = New Size(Me.TextBox.Size.Width, Me.cmb.Size.Height)
Me.cmb.SelectedIndex = Me.cmb.FindStringExact(Me.TextBox.Text)
Me.cmb.Show() Me.cmb.BringToFront() Me.cmb.Focus() End If Catch ex As Exception 'MsgBox(ex.ToString) End Try
End Sub
Protected Overrides Sub SetColumnValueAtRow(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal value As Object) Try Dim s As Object = value
Dim pt As Point = Me.DataGridTableStyle.DataGrid.PointToClient(Control.MousePosition) Dim hti As DataGrid.HitTestInfo = Me.DataGridTableStyle.DataGrid.HitTest(pt) Dim bmb As BindingManagerBase = Me.DataGridTableStyle.DataGrid.BindingContext(Me.DataGridTableStyle.DataGrid.DataSource, Me.DataGridTableStyle.DataGrid.DataMember) 'If ((hti.Row < bmb.Count) AndAlso (hti.Type = DataGrid.HitTestType.Cell)) Then
Dim cm As CurrencyManager = CType(Me.DataGridTableStyle.DataGrid.BindingContext(Me.cmb.DataSource), CurrencyManager) Dim dv As DataView = CType(cm.List, DataView) Dim i As Integer
For i = 0 To dv.Count - 1 If (s.Equals(dv(i)(Me.cmb.DisplayMember))) Then Exit For End If Next
If (i < dv.Count) Then s = dv(i)(Me.cmb.ValueMember) Else s = DBNull.Value End If MyBase.SetColumnValueAtRow(source, rowNum, s)
'End If Catch ex As Exception 'MsgBox(ex.ToString) End Try
End Sub
Protected Overrides Function GetColumnValueAtRow(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Object Try Dim obj As Object = MyBase.GetColumnValueAtRow(source, rowNum)
Dim cm As CurrencyManager = CType(Me.DataGridTableStyle.DataGrid.BindingContext(Me.cmb.DataSource), CurrencyManager) Dim dv As DataView = CType(cm.List, DataView)
Dim i As Integer For i = 0 To dv.Count - 1 If (obj.Equals(dv(i)(Me.cmb.ValueMember))) Then Exit For End If Next If (i < dv.Count) Then Return dv(i)(Me.cmb.DisplayMember) End If
Return DBNull.Value Catch ex As Exception 'MsgBox(ex.ToString) End Try
End Function
End Class
|