更改后的comboboxcolumn

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值