Imports System.Data
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Data.SqlClient
Imports System.Diagnostics
Public Class DataGridComboColumn
Inherits System.Windows.Forms.DataGridTextBoxColumn
Dim cbo As ComboBox
Dim cm As CurrencyManager
Dim iRowNumber As Integer
Public Property ComboBox() As ComboBox
Get
Return cbo
End Get
Set(ByVal Value As ComboBox)
cbo = Value
End Set
End Property
#Region " 组件设计器生成的代码 "
Public Sub New()
MyBase.New()
cm = Nothing
cbo = New ComboBox
cbo.DropDownStyle = ComboBoxStyle.DropDownList
AddHandler cbo.Leave, AddressOf cbo_leave
' 该调用是组件设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'Control 重写 dispose 以清理组件列表。
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
'控件设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是组件设计器所必需的
' 可以使用组件设计器修改此过程。不要使用
' 代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
Protected Overrides Sub Abort(ByVal rowNum As Integer)
End Sub
Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean
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)
MyBase.Edit(source, rowNum, bounds, [readOnly], instantText, cellIsVisible)
If ((Not [readOnly]) And cellIsVisible) Then
'save current row in the datagrid and currency manager associated with
'the data source for the datagrid
iRowNumber = rowNum
cm = source
AddHandler MyBase.DataGridTableStyle.DataGrid.Scroll, AddressOf DataGrid_Scroll
cbo.Parent = MyBase.TextBox.Parent
Dim rect As Rectangle = MyBase.DataGridTableStyle.DataGrid.GetCellBounds(MyBase.DataGridTableStyle.DataGrid.CurrentCell)
cbo.Location = rect.Location()
cbo.Size = New Size(MyBase.TextBox.Size.Width, MyBase.TextBox.Size.Height)
cbo.SelectedIndex = cbo.FindStringExact(MyBase.TextBox.Text)
cbo.Show()
cbo.BringToFront()
cbo.Focus()
End If
End Sub
Sub cbo_leave(ByVal sender As Object, ByVal e As EventArgs)
Dim rowView As DataRowView = CType(cbo.SelectedItem, DataRowView)
Dim s As String = CType(rowView.Row(cbo.DisplayMember), String)
MyBase.SetColumnValueAtRow(cm, iRowNumber, s)
MyBase.Invalidate()
cbo.Hide()
RemoveHandler MyBase.DataGridTableStyle.DataGrid.Scroll, AddressOf DataGrid_Scroll
'mybase.DataGridTableStyle.DataGrid
End Sub
Sub DataGrid_Scroll(ByVal sender As Object, ByVal e As EventArgs)
cbo.Hide()
End Sub
Protected Overrides Sub SetColumnValueAtRow(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal value As Object)
Dim s As Object = value
Dim c As CurrencyManager = CType(MyBase.DataGridTableStyle.DataGrid.BindingContext(cbo.DataSource), CurrencyManager)
Dim dview As DataView = CType(c.List, DataView)
Dim i As Integer = 0
While i < dview.Count
If (s = dview(i)(cbo.DisplayMember)) Then
Exit While
End If
i += 1
End While
If (i < dview.Count) Then
s = dview(i)(cbo.ValueMember)
Else
s = Nothing
End If
MyBase.SetColumnValueAtRow(source, rowNum, s)
End Sub
Protected Overrides Function GetColumnValueAtRow(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Object
Dim obj As Object = MyBase.GetColumnValueAtRow(source, rowNum)
Dim c As CurrencyManager = CType(MyBase.DataGridTableStyle.DataGrid.BindingContext(cbo.DataSource), CurrencyManager)
Dim dview As DataView = CType(c.List, DataView)
Dim i As Integer = 0
While i < dview.Count
If obj = dview(i)(cbo.ValueMember) Then
Exit While
End If
i += 1
End While
If i < dview.Count Then
Return (dview(i)(cbo.DisplayMember))
Else
Return (Nothing)
End If
End Function
End Class
真方便