zoukankan      html  css  js  c++  java
  • Datagrid 中添加ComboBox 的两种方法(winform)

    1. 继承DataGridColumnStyle完成向DataGrid中添加ComboBox。
      Public Class DataGridComboColumn
          
      Inherits DataGridColumnStyle

          
      Public WithEvents DGCombo As ComboBox = New ComboBox
          
      Private isEditing As Boolean
          
      Private _strSelectedText As String

          
      Public Sub New()
              
      MyBase.New()
              DGCombo.Visible 
      = False
          
      End Sub


          
      Protected Overrides Sub Abort(ByVal rowNum As Integer)
              isEditing 
      = False
              
      RemoveHandler DGCombo.SelectedValueChanged, AddressOf DGCombo_SelectedValueChanged
              Invalidate()

          
      End Sub


          
      Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As IntegerAs Boolean
              DGCombo.Bounds 
      = Rectangle.Empty
              
      AddHandler DGCombo.SelectedValueChanged, AddressOf DGCombo_SelectedValueChanged
              
      If isEditing = False Then
                  
      Return True
              
      End If

              isEditing 
      = False
              
      Try
                  DGCombo.Text 
      = DGCombo.Text
              
      Catch ex As Exception
                  DGCombo.Text 
      = String.Empty
              
      End Try

              
      Try
                  
      Dim value As String = _strSelectedText
                  SetColumnValueAtRow(dataSource, rowNum, value)
              
      Catch ex As Exception
                  Abort(rowNum)
                  
      Return False
              
      End Try
              Invalidate()
              
      Return True

          
      End Function


          
      Protected Overloads Overrides 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)
              
      Dim value As String
              
      Try
                  value 
      = CType(GetColumnValueAtRow(source, rowNum), String)
              
      Catch ex As Exception
                  SetColumnValueAtRow(source, rowNum, DGCombo.Text)
              
      End Try

              value 
      = CType(GetColumnValueAtRow(source, rowNum), String)

              
      If (cellIsVisible) Then
                  DGCombo.Bounds 
      = New Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height)
                  DGCombo.Text 
      = value
                  DGCombo.Visible 
      = True
                  
      AddHandler DGCombo.SelectedValueChanged, AddressOf DGCombo_SelectedValueChanged
              
      Else
                  DGCombo.Text 
      = value
                  DGCombo.Visible 
      = False
              
      End If

              
      If DGCombo.Visible = False Then
                  DataGridTableStyle.DataGrid.Invalidate(bounds)
              
      End If
          
      End Sub


          
      Protected Overrides Function GetMinimumHeight() As Integer

          
      End Function


          
      Protected Overrides Function GetPreferredHeight(ByVal g As System.Drawing.Graphics, ByVal value As ObjectAs Integer

          
      End Function


          
      Protected Overrides Function GetPreferredSize(ByVal g As System.Drawing.Graphics, ByVal value As ObjectAs System.Drawing.Size

          
      End Function


          
      Protected Overloads Overrides 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)
              Paint(g, bounds, source, rowNum, 
      True)
          
      End Sub


          
      Protected Overloads Overrides 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)
              Paint(g, bounds, source, rowNum, Brushes.Red, Brushes.Blue, alignToRight)
          
      End Sub


          
      Protected Overloads Overrides 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, _
                                    
      ByVal backBrush As System.Drawing.Brush, _
                                    
      ByVal foreBrush As System.Drawing.Brush, _
                                    
      ByVal alignToRight As Boolean)

              
      Dim strDate As String
              
      Dim rect As RectangleF
              
      Try
                  strDate 
      = DGCombo.Text
                  strDate 
      = CType(GetColumnValueAtRow(source, rowNum), String)
              
      Catch ex As Exception
                  SetColumnValueAtRow(source, rowNum, DGCombo.Text)
                  strDate 
      = CType(GetColumnValueAtRow(source, rowNum), String)
              
      End Try

              rect.X 
      = bounds.X
              rect.Y 
      = bounds.Y
              rect.Height 
      = bounds.Height
              rect.Width 
      = bounds.Width

              g.FillRectangle(backBrush, rect)
              rect.Offset(
      02)
              rect.Height 
      -= 2

              g.DrawString(strDate, 
      Me.DataGridTableStyle.DataGrid.Font, foreBrush, rect)

          
      End Sub


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


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

      End Class



      以下是使用方法!

          
      Private Sub frmDataGrid_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles MyBase.Load
              
      Call TEST()
          
      End Sub


          
      Private Sub TEST()
              
      Dim DT As New DataTable("TEST")
              DT.Columns.Add(
      New DataColumn("ID"))
              DT.Columns.Add(
      New DataColumn("COMBO"))
              
      Call AddGridStyle()
              
      Call ADDDATA(DT)
              DBGrid.DataSource 
      = DT

          
      End Sub


          
      Private Sub AddGridStyle()

              
      Dim DBGridStyle As DataGridTableStyle
              
      Dim IDColumn As DataGridTextBoxColumn
              
      Dim DCColumn As DataGridComboColumn


              DBGridStyle 
      = New DataGridTableStyle
              DBGridStyle.MappingName 
      = "TEST"

              IDColumn 
      = New DataGridTextBoxColumn
              IDColumn.MappingName 
      = "ID"
              IDColumn.HeaderText 
      = "ID"
              IDColumn.Alignment 
      = HorizontalAlignment.Center
              IDColumn.Width 
      = 50
              DBGridStyle.GridColumnStyles.Add(IDColumn)

             DCColumn 
      = New DataGridComboColumn
              
      Dim i As Integer
              
      For i = 0 To 25
                  DCColumn.DGCombo.Items.Add((i 
      + 1).ToString("00000"))
              
      Next
              DCColumn.DGCombo.DropDownWidth 
      = 120
              DCColumn.MappingName 
      = "COMBO"
              DCColumn.HeaderText 
      = "COMBO"
              DCColumn.Alignment 
      = HorizontalAlignment.Center
              DCColumn.Width 
      = 60
              DBGridStyle.GridColumnStyles.Add(DCColumn)

             DBGrid.TableStyles.Add(DBGridStyle)

          
      End Sub


          
      Private Sub ADDDATA(ByRef DT As DataTable)
              
      Dim DROW As DataRow
              
      Dim intRow As Integer

              
      For intRow = 0 To 9
                  DROW 
      = DT.NewRow()
                  DROW.Item(
      "ID"= Format(intRow + 1"000")
                  DROW.Item(
      "COMBO"= Format(intRow + 1"00000")
                  DT.Rows.Add(DROW)
              
      Next

              DT.AcceptChanges()

          
      End Sub



    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 ObjectByVal 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.

  • 相关阅读:
    Echarts之美国新冠疫情地图制作
    前端三件套--CSS(pink老师课程笔记)
    数据预处理与转存(Hive--Mysql)
    HDFS的命令行使用
    hdfs的写入过程和读取过程
    树(一)树的基本知识
    指针中容易混淆的概念
    顺序查找、单链表查找、折半查找
    JAVA之I/O流
    SpringBoot-自定义spring-boot-starter
  • 原文地址:https://www.cnblogs.com/asyuras/p/263778.html
Copyright © 2011-2022 走看看