Datagrid 中添加ComboBox 的两种方法(winform)

  1. 继承DataGridColumnStyle完成向DataGrid中添加ComboBox。
    ExpandedBlockStart.gif ContractedBlock.gif Public   Class DataGridComboColumn Class DataGridComboColumn
    InBlock.gif    
    Inherits DataGridColumnStyle
    InBlock.gif
    InBlock.gif    
    Public WithEvents DGCombo As ComboBox = New ComboBox
    InBlock.gif    
    Private isEditing As Boolean
    InBlock.gif    
    Private _strSelectedText As String
    InBlock.gif
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    Public Sub New()Sub New()
    InBlock.gif        
    MyBase.New()
    InBlock.gif        DGCombo.Visible 
    = False
    ExpandedSubBlockEnd.gif    
    End Sub

    InBlock.gif
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    Protected Overrides Sub Abort()Sub Abort(ByVal rowNum As Integer)
    InBlock.gif        isEditing 
    = False
    InBlock.gif        
    RemoveHandler DGCombo.SelectedValueChanged, AddressOf DGCombo_SelectedValueChanged
    InBlock.gif        Invalidate()
    InBlock.gif
    ExpandedSubBlockEnd.gif    
    End Sub

    InBlock.gif
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    Protected Overrides Function Commit()Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As IntegerAs Boolean
    InBlock.gif        DGCombo.Bounds 
    = Rectangle.Empty
    InBlock.gif        
    AddHandler DGCombo.SelectedValueChanged, AddressOf DGCombo_SelectedValueChanged
    InBlock.gif        
    If isEditing = False Then
    InBlock.gif            
    Return True
    InBlock.gif        
    End If
    InBlock.gif
    InBlock.gif        isEditing 
    = False
    InBlock.gif        
    Try
    InBlock.gif            DGCombo.Text 
    = DGCombo.Text
    InBlock.gif        
    Catch ex As Exception
    InBlock.gif            DGCombo.Text 
    = String.Empty
    InBlock.gif        
    End Try
    InBlock.gif
    InBlock.gif        
    Try
    InBlock.gif            
    Dim value As String = _strSelectedText
    InBlock.gif            SetColumnValueAtRow(dataSource, rowNum, value)
    InBlock.gif        
    Catch ex As Exception
    InBlock.gif            Abort(rowNum)
    InBlock.gif            
    Return False
    InBlock.gif        
    End Try
    InBlock.gif        Invalidate()
    InBlock.gif        
    Return True
    InBlock.gif
    ExpandedSubBlockEnd.gif    
    End Function

    InBlock.gif
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    Protected Overloads Overrides Sub Edit()Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As IntegerByVal bounds As System.Drawing.Rectangle, ByVal [readOnlyAs BooleanByVal instantText As StringByVal cellIsVisible As Boolean)
    InBlock.gif        
    Dim value As String
    InBlock.gif        
    Try
    InBlock.gif            value 
    = CType(GetColumnValueAtRow(source, rowNum), String)
    InBlock.gif        
    Catch ex As Exception
    InBlock.gif            SetColumnValueAtRow(source, rowNum, DGCombo.Text)
    InBlock.gif        
    End Try
    InBlock.gif
    InBlock.gif        value 
    = CType(GetColumnValueAtRow(source, rowNum), String)
    InBlock.gif
    InBlock.gif        
    If (cellIsVisible) Then
    InBlock.gif            DGCombo.Bounds 
    = New Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height)
    InBlock.gif            DGCombo.Text 
    = value
    InBlock.gif            DGCombo.Visible 
    = True
    InBlock.gif            
    AddHandler DGCombo.SelectedValueChanged, AddressOf DGCombo_SelectedValueChanged
    InBlock.gif        
    Else
    InBlock.gif            DGCombo.Text 
    = value
    InBlock.gif            DGCombo.Visible 
    = False
    InBlock.gif        
    End If
    InBlock.gif
    InBlock.gif        
    If DGCombo.Visible = False Then
    InBlock.gif            DataGridTableStyle.DataGrid.Invalidate(bounds)
    InBlock.gif        
    End If
    ExpandedSubBlockEnd.gif    
    End Sub

    InBlock.gif
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    Protected Overrides Function GetMinimumHeight()Function GetMinimumHeight() As Integer
    InBlock.gif
    ExpandedSubBlockEnd.gif    
    End Function

    InBlock.gif
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    Protected Overrides Function GetPreferredHeight()Function GetPreferredHeight(ByVal g As System.Drawing.Graphics, ByVal value As ObjectAs Integer
    InBlock.gif
    ExpandedSubBlockEnd.gif    
    End Function

    InBlock.gif
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    Protected Overrides Function GetPreferredSize()Function GetPreferredSize(ByVal g As System.Drawing.Graphics, ByVal value As ObjectAs System.Drawing.Size
    InBlock.gif
    ExpandedSubBlockEnd.gif    
    End Function

    InBlock.gif
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    Protected Overloads Overrides Sub Paint()Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer)
    InBlock.gif        Paint(g, bounds, source, rowNum, 
    True)
    ExpandedSubBlockEnd.gif    
    End Sub

    InBlock.gif
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    Protected Overloads Overrides Sub Paint()Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As IntegerByVal alignToRight As Boolean)
    InBlock.gif        Paint(g, bounds, source, rowNum, Brushes.Red, Brushes.Blue, alignToRight)
    ExpandedSubBlockEnd.gif    
    End Sub

    InBlock.gif
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    Protected Overloads Overrides Sub Paint()Sub Paint(ByVal g As System.Drawing.Graphics, _
    InBlock.gif                              
    ByVal bounds As System.Drawing.Rectangle, _
    InBlock.gif                              
    ByVal source As System.Windows.Forms.CurrencyManager, _
    InBlock.gif                              
    ByVal rowNum As Integer, _
    InBlock.gif                              
    ByVal backBrush As System.Drawing.Brush, _
    InBlock.gif                              
    ByVal foreBrush As System.Drawing.Brush, _
    InBlock.gif                              
    ByVal alignToRight As Boolean)
    InBlock.gif
    InBlock.gif        
    Dim strDate As String
    InBlock.gif        
    Dim rect As RectangleF
    InBlock.gif        
    Try
    InBlock.gif            strDate 
    = DGCombo.Text
    InBlock.gif            strDate 
    = CType(GetColumnValueAtRow(source, rowNum), String)
    InBlock.gif        
    Catch ex As Exception
    InBlock.gif            SetColumnValueAtRow(source, rowNum, DGCombo.Text)
    InBlock.gif            strDate 
    = CType(GetColumnValueAtRow(source, rowNum), String)
    InBlock.gif        
    End Try
    InBlock.gif
    InBlock.gif        rect.X 
    = bounds.X
    InBlock.gif        rect.Y 
    = bounds.Y
    InBlock.gif        rect.Height 
    = bounds.Height
    InBlock.gif        rect.Width 
    = bounds.Width
    InBlock.gif
    InBlock.gif        g.FillRectangle(backBrush, rect)
    InBlock.gif        rect.Offset(
    02)
    InBlock.gif        rect.Height 
    -= 2
    InBlock.gif
    InBlock.gif        g.DrawString(strDate, 
    Me.DataGridTableStyle.DataGrid.Font, foreBrush, rect)
    InBlock.gif
    ExpandedSubBlockEnd.gif    
    End Sub

    InBlock.gif
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    Protected Overrides Sub SetDataGridInColumn()Sub SetDataGridInColumn(ByVal value As DataGrid)
    InBlock.gif        
    MyBase.SetDataGridInColumn(value)
    InBlock.gif        
    If Not (DGCombo.Parent Is NothingThen
    InBlock.gif            DGCombo.Parent.Controls.Remove(DGCombo)
    InBlock.gif        
    End If
    InBlock.gif        
    If Not (value Is NothingThen
    InBlock.gif            value.Controls.Add(DGCombo)
    InBlock.gif        
    End If
    ExpandedSubBlockEnd.gif    
    End Sub

    InBlock.gif
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    Private Sub DGCombo_SelectedValueChanged()Sub DGCombo_SelectedValueChanged(ByVal sender As ObjectByVal e As System.EventArgs) Handles DGCombo.SelectedValueChanged
    InBlock.gif        isEditing 
    = True
    InBlock.gif        
    MyBase.ColumnStartedEditing(DGCombo)
    InBlock.gif        _strSelectedText 
    = DGCombo.Text
    InBlock.gif        
    If _strSelectedText Is Nothing Then
    InBlock.gif            _strSelectedText 
    = String.Empty
    InBlock.gif        
    End If
    ExpandedSubBlockEnd.gif    
    End Sub

    ExpandedBlockEnd.gif
    End Class

    None.gif
    None.gif
    None.gif以下是使用方法!
    None.gif
    ExpandedBlockStart.gifContractedBlock.gif    
    Private   Sub frmDataGrid_Load() Sub frmDataGrid_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles MyBase.Load
    InBlock.gif        
    Call TEST()
    ExpandedBlockEnd.gif    
    End Sub

    None.gif
    ExpandedBlockStart.gifContractedBlock.gif    
    Private   Sub TEST() Sub TEST()
    InBlock.gif        
    Dim DT As New DataTable("TEST")
    InBlock.gif        DT.Columns.Add(
    New DataColumn("ID"))
    InBlock.gif        DT.Columns.Add(
    New DataColumn("COMBO"))
    InBlock.gif        
    Call AddGridStyle()
    InBlock.gif        
    Call ADDDATA(DT)
    InBlock.gif        DBGrid.DataSource 
    = DT
    InBlock.gif
    ExpandedBlockEnd.gif    
    End Sub

    None.gif
    ExpandedBlockStart.gifContractedBlock.gif    
    Private   Sub AddGridStyle() Sub AddGridStyle()
    InBlock.gif
    InBlock.gif        
    Dim DBGridStyle As DataGridTableStyle
    InBlock.gif        
    Dim IDColumn As DataGridTextBoxColumn
    InBlock.gif        
    Dim DCColumn As DataGridComboColumn
    InBlock.gif
    InBlock.gif
    InBlock.gif        DBGridStyle 
    = New DataGridTableStyle
    InBlock.gif        DBGridStyle.MappingName 
    = "TEST"
    InBlock.gif
    InBlock.gif        IDColumn 
    = New DataGridTextBoxColumn
    InBlock.gif        IDColumn.MappingName 
    = "ID"
    InBlock.gif        IDColumn.HeaderText 
    = "ID"
    InBlock.gif        IDColumn.Alignment 
    = HorizontalAlignment.Center
    InBlock.gif        IDColumn.Width 
    = 50
    InBlock.gif        DBGridStyle.GridColumnStyles.Add(IDColumn)
    InBlock.gif
    InBlock.gif       DCColumn 
    = New DataGridComboColumn
    InBlock.gif        
    Dim i As Integer
    InBlock.gif        
    For i = 0 To 25
    InBlock.gif            DCColumn.DGCombo.Items.Add((i 
    + 1).ToString("00000"))
    InBlock.gif        
    Next
    InBlock.gif        DCColumn.DGCombo.DropDownWidth 
    = 120
    InBlock.gif        DCColumn.MappingName 
    = "COMBO"
    InBlock.gif        DCColumn.HeaderText 
    = "COMBO"
    InBlock.gif        DCColumn.Alignment 
    = HorizontalAlignment.Center
    InBlock.gif        DCColumn.Width 
    = 60
    InBlock.gif        DBGridStyle.GridColumnStyles.Add(DCColumn)
    InBlock.gif
    InBlock.gif       DBGrid.TableStyles.Add(DBGridStyle)
    InBlock.gif
    ExpandedBlockEnd.gif    
    End Sub

    None.gif
    ExpandedBlockStart.gifContractedBlock.gif    
    Private   Sub ADDDATA() Sub ADDDATA(ByRef DT As DataTable)
    InBlock.gif        
    Dim DROW As DataRow
    InBlock.gif        
    Dim intRow As Integer
    InBlock.gif
    InBlock.gif        
    For intRow = 0 To 9
    InBlock.gif            DROW 
    = DT.NewRow()
    InBlock.gif            DROW.Item(
    "ID"= Format(intRow + 1"000")
    InBlock.gif            DROW.Item(
    "COMBO"= Format(intRow + 1"00000")
    InBlock.gif            DT.Rows.Add(DROW)
    InBlock.gif        
    Next
    InBlock.gif
    InBlock.gif        DT.AcceptChanges()
    InBlock.gif
    ExpandedBlockEnd.gif    
    End Sub

    None.gif
    None.gif
    None.gif
  2. vb6
    要在Datagrid 中添加ComboBox,擦采用如下方法:
      
    Dim  MyCombo  As   New  ComboBox
         
    添加到Datagrid格式化中
     
    AddHandler  MyCombo.TextChanged,  AddressOf  Ctrls_TextChanged
            
    ' 设置MyCombo的Name以及Visible属性
            MyCombo.Name  =   " MyCombo "
            MyCombo.Visible 
    =   False
            MyCombo.DropDownStyle 
    =  ComboBoxStyle.DropDown
            
    ' 清空MyCombo
            MyCombo.Items.Clear()
            
    ' 给MyCombo添加项
            Me .MyCombo.Items.Add( "   " )

            
    ' 把MyCombo加入到dgdGoodInfo的Controls集合中
            dgdGoodInfo.Controls.Add(MyCombo)

    添加函数,MyCombo 添加到第四列
    Private   Sub  Ctrls_TextChanged( ByVal  sender  As   Object , _
        
    ByVal  e  As  System.EventArgs)
            
    ' 判断DataGrid的当前单元格是否属于第四列
             ' DataGrid的列是从0开始
             If  dgdInfo.CurrentCell.ColumnNumber  =   4   Then
                
    If  dgdInfo.Item(dgdInfo.CurrentCell)  &   ""   =   ""   Then
                    SendKeys.Send(
    "   " )
                
    End   If
                
    ' 设置当前单元格的值为MyCombo选中的项的Text
                dgdInfo.Item(dgdInfo.CurrentCell)  =  MyCombo.Text
            
    End   If
        
    End Sub

        
    Private   Sub  dgdInfo_Click( ByVal  sender  As   Object , _
        
    ByVal  e  As  System.EventArgs)  Handles  dgdInfo.Click
            
    ' 设置MyCombo的Visible,Width属性
            MyCombo.Visible  =   False
            MyCombo.Width 
    =   0
        
    End Sub

        
    Private   Sub  dgdInfo_CurrentCellChanged( ByVal  sender _
        
    As   Object ByVal  e  As  System.EventArgs)  Handles  _
        dgdInfo.CurrentCellChanged
            
    If  dgdInfo.CurrentCell.ColumnNumber  =   4   Then
                MyCombo.Visible 
    =   False
                MyCombo.Width 
    =   0
                MyCombo.Left 
    =  dgdInfo.GetCurrentCellBounds.Left
                MyCombo.Top 
    =  dgdInfo.GetCurrentCellBounds.Top
                MyCombo.Text 
    =  _
                dgdGoodInfo.Item(dgdInfo.CurrentCell) 
    &   ""
                MyCombo.Visible 
    =   True
            
    Else
                MyCombo.Visible 
    =   False
                MyCombo.Width 
    =   0
            
    End   If
        
    End Sub

        
    Private   Sub  dgdInfo_Paint( ByVal  sender  As   Object , _
        
    ByVal  e  As  PaintEventArgs)  Handles  dgdGoodInfo.Paint
            
    If  dgdInfo.CurrentCell.ColumnNumber  =   4   Then
                MyCombo.Width 
    =  dgdInfo.GetCurrentCellBounds.Width
            
    End   If
        
    End Sub

        
    Private   Sub  dgdInfo_Scroll( ByVal  sender  As   Object , _
        
    ByVal  e  As  System.EventArgs)  Handles  dgdInfo.Scroll
            MyCombo.Visible 
    =   False
            MyCombo.Width 
    =   0
        
    End Sub
    注:dgdInfo 为DataGrid.

转载于:https://www.cnblogs.com/asyuras/archive/2005/10/28/263778.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值