Imports System.Data
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Data.SqlClient
Imports System.Diagnostics
Namespace BaseDocumentsControls
Public Class DataGridComboColumn
Inherits System.Windows.Forms.DataGridTextBoxColumn
Dim cbo As ComboBox
Dim cm As CurrencyManager
Dim iRowNumber As Integer
Dim isCellChanged As Boolean = False
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
'AddHandler cbo.SelectedIndexChanged, AddressOf SelectedItem_changed
' 该调用是组件设计器所必需的。
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 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)
If Not isCellChanged Then
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
AddHandler MyBase.DataGridTableStyle.DataGrid.CurrentCellChanged, AddressOf Cell_Changed
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)
MyBase.TextBox.Hide()
cbo.SelectedIndex = cbo.FindStringExact(MyBase.TextBox.Text)
cbo.Show()
cbo.BringToFront()
cbo.Focus()
isCellChanged = True
End If
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
' If Not IsNothing(rowView) Then
' s = CType(rowView.Row(cbo.DisplayMember) & "", String)
' Else
' s = ""
' End If
If isCellChanged Then
SetColumnValueAtRow(cm, iRowNumber, cbo.Text)
' MyBase.Invalidate()
cbo.Hide()
MyBase.TextBox.BringToFront()
MyBase.TextBox.Focus()
MyBase.Commit(cm, iRowNumber)
isCellChanged = False
RemoveHandler MyBase.DataGridTableStyle.DataGrid.Scroll, AddressOf DataGrid_Scroll
End If
'mybase.DataGridTableStyle.DataGrid
End Sub
Sub DataGrid_Scroll(ByVal sender As Object, ByVal e As EventArgs)
If isCellChanged Then
isCellChanged = False
MyBase.EndEdit()
cbo.Hide()
End If
End Sub
Sub Cell_Changed(ByVal sender As Object, ByVal e As EventArgs)
If isCellChanged Then
'正在编辑中
isCellChanged = False
MyBase.EndEdit()
cbo.Hide()
MyBase.TextBox.Focus()
End If
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.Equals(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 = DBNull.Value
' End If
MyBase.SetColumnValueAtRow(source, rowNum, value)
End Sub
Sub SelectedItem_changed(ByVal sender As Object, ByVal e As EventArgs)
isCellChanged = True
MyBase.SetColumnValueAtRow(cm, iRowNumber, cbo.Text)
MyBase.Commit(cm, iRowNumber)
End Sub
End Class
End Namespace