向DataGrid控件中添加ComboBox控件

原创 2005年05月16日 12:05:00

在前面看到了很多关于怎样向DataGrid中添加ComboBox控件的方法。使用的方法全部都是在VB6.0中的方法。

我还是要说说在CSND中发贴的朋友。

现在所谓的.NET编程人员,不知道是怎么了呢!只是停留在使用.NET的编程环境中。并没有真正的了解面向对象的.NET编程思想。

我现在就利用继承DataGridColumnStyle完成向DataGrid中添加ComboBox。

希望这样有助于大家了解真正的面向对象编程的思想。不要只是认为利用VB6.0中的某些方法就是.NET高手了:)下面是实现代码:我使用的是VB.NET来完成的。

我熟悉C#,但是VB.NET只是大概了解一下。:)

由于时间关系没有协注释,请见谅!

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 Integer) As 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 Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal 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 Object) As Integer

    End Function

    Protected Overrides Function GetPreferredSize(ByVal g As System.Drawing.Graphics, ByVal value As Object) As 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 Integer, ByVal 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(0, 2)
        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 Nothing) Then
            DGCombo.Parent.Controls.Remove(DGCombo)
        End If
        If Not (value Is Nothing) Then
            value.Controls.Add(DGCombo)
        End If
    End Sub

    Private Sub DGCombo_SelectedValueChanged(ByVal sender As Object, ByVal 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 Object, ByVal 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

那么随时在DataGrid中所指定为Combo列中单击。Combo就出现了

 

Winform DataGrid设计三步曲之二------如何在DataGrid中加入ComboBox控件

          Winform DataGrid设计三步曲之二                        如何在DataGrid中加入ComboBox控件        C#类库中的DataG...
  • tjvictor
  • tjvictor
  • 2007年01月22日 11:25
  • 5741

Datagrid 中添加ComboBox

Datagrid的列支持两种样式,分别是DatagridTextColumn和DatagridBoolColumn,前者表现为单元格时为Textbox ,后者表现为单元格时为一个CheckBox.要在...
  • wuyq11
  • wuyq11
  • 2005年09月24日 11:14
  • 2917

在DataGrid中使用下拉列表ComboBox控件

在使用Microsoft.Net Framework环境下编写数据库方面的软件时,经常要用到DataGrid数据显示控件,但同时也发现,DataGrid提供的网格显示中,只有两种:一种是普通的Text...
  • chenguang79
  • chenguang79
  • 2006年03月24日 10:55
  • 2265

DataGrid中的子控件Combox之数据源绑定(WPF)

看到同事有一个这样的需求,自己就写了一个小例子 如何根据DataGrid中Combox中选择的值将对应的数据显示到DataGrid中 创建了 三个数据源   List SelectionList...
  • baiduandxunlei
  • baiduandxunlei
  • 2013年03月17日 21:45
  • 9448

c#(winform)中ComboBox和ListBox添加项,写一个用户控件

用comboBox的数据绑定的方法很简单,建一个数据源,绑定到ComboBox上,然后指定DisplayMember和 ValueMember就可以了。但是感觉好不灵活哦,如果我要在ComboBox上...
  • u013908405
  • u013908405
  • 2015年08月18日 14:19
  • 1200

Qt:TreeWidget中添加子控件,并在主窗口接收响应

头文件: mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include #include namespa...
  • biu__biu_biu
  • biu__biu_biu
  • 2017年10月15日 18:06
  • 358

【VC+MFC】在工具栏上添加ComboBox控件的方法

最近在做一个软件开发,需要在工具栏上添加
  • Siufei_Au
  • Siufei_Au
  • 2014年08月08日 11:19
  • 639

有关ActiveX控件中的DataGrid或ADO Data控件在vc++6.0和VS 版本中的使用

系统环境:Windows 7 + VC6.0 + VS2008 当在VC++6.0中"Project"-->"Add to Project"-->"Componets and Controls"--...
  • zhangyulin54321
  • zhangyulin54321
  • 2012年10月30日 16:54
  • 5942

Windows 窗体 DataGrid 控件添加表和列

Windows 窗体 DataGrid 控件添加表和列 =========================================================== 通过创建 DataGri...
  • jupiter97
  • jupiter97
  • 2007年05月17日 12:01
  • 883

VC DataGrid控件使用总结

MS CDataGrid控件用法详解 本文示例源代码或素材下载 。DataGrid控件是VC方便地用来显示数据的一个极好的网格控件,我不否认网上还有期它不少优秀的网格控件,但我总喜欢使用微...
  • bcbobo21cn
  • bcbobo21cn
  • 2016年04月01日 18:37
  • 1758
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:向DataGrid控件中添加ComboBox控件
举报原因:
原因补充:

(最多只允许输入30个字)