向DataGrid控件中添加ComboBox控件

combobox控件|datagrid|datagrid控件

 
在前面看到了很多关于怎样向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就出现了

时间: 2024-12-03 12:13:08

向DataGrid控件中添加ComboBox控件的相关文章

嵌入式开发-在Window CE 操作系统中,如何在DataGrid 中添加ComboBox 控件

问题描述 在Window CE 操作系统中,如何在DataGrid 中添加ComboBox 控件 最近在开发手持机,因为公司要求,将ERP 系统置入到手持机中,发现在Window CE 系统中,很多 控件都无法使用.譬如说,要在DataGrid 中添加列,要求在DataGrid 中操作数据,下拉的 ComBobox 就没有,也没有DataGridComBoBox 列,如何处理,请各位兄弟指教. QQ:870088133 解决方案 CE版的控件是精简的,很多功能都没有.需要自己实现. 你说的添加C

java-怎样在easyui中的datagrid的列中添加combobox并动态获取数据到combobox中?

问题描述 怎样在easyui中的datagrid的列中添加combobox并动态获取数据到combobox中? <table id="data" class="easyui-datagrid" style="width:700px;height:250px" url="getAllServer.do" data-options="pageSize:5 ,pageList: [3,5,10]" tool

在UpdatePanel控件中使用验证控件

在本演练中,您将在 UpdatePanel 控件中使用验证控件以在浏览器中执行验证. 所用示例是一个简化的票证查询系统.用户可以指定他们所需的日期和票证数目.当用户提交该页时,该页将指示是否存在可用票证. 接受用户输入的控件位于 UpdatePanel 控件中.若要确保用户只输入有效的值,则还需要在 UpdatePanel 控件中添加验证控件. UpdatePanel 控件中的按钮可执行异步回发,但仅在用户输入有效时才起作用.当在浏览器中验证成功后,将执行异步回发并刷新面板的内容. 先决条件 若

datepicker-silverlight的DataGrid中添加DatePicker控件使用滚动条滚动数据有误

问题描述 silverlight的DataGrid中添加DatePicker控件使用滚动条滚动数据有误 silverlight的DataGrid中添加DatePicker控件,初始设定DatePicker中的时间,连续使用滚动条滚动,初始设定DatePicker的值会随机改变. <sdk:DataGrid Grid.Row="1" HorizontalAlignment="Stretch" Name="dataGrid1" Vertical

ASP.NET开发系列之在用户控件中添加事件

asp.net|控件 在<在用户控件中添加属性>这一篇文章中我们演示了如何在用户控件中添加属性,接下来我们演示如何在用户控件中添加事件. 在<在用户控件中添加属性>这一篇文章中我们定义了一个用户登录的用户控件UserLogin.ascx 文件,里面包含了一个LinkButton服务器按钮控件,当用户单击该按钮时服务器端会自动生成一个回发来激发Page.Load事件.除了服务器自动产生回发来激发Page.Load事件外,我们可以给LinkButton添加一个它自己的事件,添加事件其实

手势左右 误判-WP 开发中在WebBrowser控件中添加手势出现误判的问题

问题描述 WP 开发中在WebBrowser控件中添加手势出现误判的问题 在xaml中对WebBrowser控件使用GestureListener实现左右滑动返回上一页.前进下一页.但是在加载2048网页版游戏的时候,出现手势误判,在玩2048游戏向右滑动的时候,会返回上一页,我想监听WebBrowser中是否有2048这类的滑动事件,应该怎么做呢? 我的xaml代码: <phone:WebBrowser VerticalAlignment="Top" x:Name="

VB 6.0关于在菜单控件中添加一个超级链接的问题

问题描述 VB 6.0关于在菜单控件中添加一个超级链接的问题 在VB 6.0编程系统中怎么在主菜单条控件上增加一个超级链接,点超级链接就打开公司的购买网站页面? 解决方案 可以把超链接放在coolbar的带区上,把菜单条也放入coolbar实现.

串口通信-VB在components中添加MSComm控件

问题描述 VB在components中添加MSComm控件 VB中在components添加mscomm控件时为什么会显示"--could not be loaded"??求解...谢谢 解决方案 VB.NET直接用SerialPort控件. 解决方案二: MFC添加两个MSComm控件后提示'CMSComm' : 'class' type redefinition

我在coreldraw vba中添加mschart控件,结果显示不受信任

问题描述 我在coreldraw vba中添加mschart控件,结果显示不受信任 我在coreldraw vba中添加mschart控件,结果显示不受信任