根据C#代码重写了datagridcomboxcolumn,发现这个还是挺好的

 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

真方便

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值